perfreduce pthread_cond_signal via wait counter

Posted albumcover

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了perfreduce pthread_cond_signal via wait counter相关的知识,希望对你有一定的参考价值。

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 }

 

以上是关于perfreduce pthread_cond_signal via wait counter的主要内容,如果未能解决你的问题,请参考以下文章

关于pthread_cond_wait()的疑问