简体   繁体   English

如果pthread_cond_wait在它之前发出信号时,是否会丢失信号

[英]if pthread_cond_wait can loss signal when pthread_cond_signal signal before it

================thread1 ================ thread1

pthread_mutex_lock(&mutex);
do 
{
    fun();//this fun will cost a long time, maybe 1-2 second
    gettimeofday(&now, NULL);
    outtime.tv_sec = now.tv_sec + 5;
    outtime.tv_nsec = now.tv_usec * 1000;
    int ret = pthread_cond_timedwait(&cond, &mutex, &outtime);    
} while((!predicate && ret != ETIMEDOUT)
pthread_mutex_unlock(&mutex);

==========================thread2 ========================= thread2

pthread_mutex_lock(&mutex);
predicate = true;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);

if thread2 send a signal during thread1's fun(), there is no pthread_cond_timedwait, when fun() call return, the phread_cond_timedwait in thread1 still can get the signal that thread2 has sent before or not? 如果线程2在线程1的fun()期间发送信号,则没有pthread_cond_timedwait,当fun()调用返回时,线程1中的phread_cond_timedwait仍然可以获得线程2之前发送或未发送的信号吗? can we call a time-consuming fun before pthread_cond_timedwait in while()?? 我们可以在while()中的pthread_cond_timedwait之前调用耗时的乐趣吗?

If pthread_cond_signal is called when no thread is waiting for a signal it has no effect. 如果在没有线程等待信号的情况下调用pthread_cond_signal则它将无效。 The signal is not stored for future use. 信号未存储以备将来使用。

If predicate is true, the code in the loop should not wait for the condition variable. 如果predicate为true,则循环中的代码不应等待条件变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM