码迷,mamicode.com
首页 > 其他好文 > 详细

【perf】reduce pthread_cond_signal via wait counter

时间:2018-07-20 00:15:28      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:sig   read   str   bsp   which   class   har   style   nbsp   

pthread_cond_signal is not needed in case that no thread is blocked on the cond var.

Take a inter-thread shared queue as an example,  which maintains a counter couting blocked threads

 

 1 void pop(void **data) {
 2     pthread_mutex_lock(&mtx);
 3     while (queue_size == 0) {
 4         count++;
 5         pthread_cond_wait(&cv, &mtx);
 6         count--;
 7     }
 8     // ... pop the front item into data
 9     pthread_mutex_lock(&mtx);
10 }
11 
12 void push(void *data) {
13     pthread_mutex_lock(&mtx);
14     // ... push data into queue
15     if (count > 0) {
16         pthread_cond_signal(&cv);
17     }
18     pthread_mutex_lock(&mtx);
19 }

 

【perf】reduce pthread_cond_signal via wait counter

标签:sig   read   str   bsp   which   class   har   style   nbsp   

原文地址:https://www.cnblogs.com/albumcover/p/9339059.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!