简体   繁体   English

pthread_cond_wait无法解锁互斥锁

[英]pthread_cond_wait does not unlock the mutex

In the first place, my problem is different. 首先,我的问题不同。

In my scenario, there is one waiting thread, which waits on condition variable. 在我的场景中,有一个等待线程,它在条件变量上等待。 Signalling thread signals conditional variable. 信号线程发出条件变量信号。

My code is 我的代码是

//Wating thread

//Lock the mutex_
//mutex_ is of pthread_mutex_t  and is initialized.
result = pthread_mutex_lock(&mutex_);
assert(result == 0);

do {
    //Wait on condition variable cvar_
    //cva_ is of pthread_cond_t  and is initialized.
    result = pthread_cond_wait(&cvar_, &mutex_);  //POINT 1
}while(result == 0 && !state_);

//Unlock the mutex_.
result = pthread_mutex_unlock(&mutex_);

//signalling thread
result = pthread_mutex_lock(&mutex_); //POINT 2
assert(result == 0);
state_ = 1;
result = pthread_mutex_unlock(&mutex_);
assert(result == 0);

//signals the condition variable.
pthread_cond_signal(&cvar_);

My operating system is Mac OS X 10.8 but minimum target is 10.6 我的操作系统是Mac OS X 10.8,但最低目标是10.6
This is running fine without any problem in almost every cases except one. 除一种情况外,几乎在所有情况下,此方法运行都没有问题。

In a particular case, I notices that after POINT 1 that is pthread_cond_wait , mutex_ is not unlocked when it enters into wait state. 在特定情况下,我注意到在POINT 1(即pthread_cond_wait ,mutex_进入等待状态时并未解锁。 This I confirmed by pthread_mutex_trylock which returns EBUSY in this case. 我通过pthread_mutex_trylock确认了这一点,在这种情况下,该线程返回EBUSY。 And due to this, signalling thread goes into wait and ultimately results in a deadlock. 因此,信令线程进入等待状态并最终导致死锁。

I would like to know under what condition, pthread_cond_wait does not unlock the mutex passed to it. 我想知道在什么情况下pthread_cond_wait不会解锁传递给它的互斥锁。 What is the cause of this problem? 这个问题是什么原因造成的?

As @KenThomases pointed out: your problem is that you are missing the signal, not that the signal isn't getting sent. 正如@KenThomases指出的那样:您的问题是您丢失了信号,而不是信号没有得到发送。 The signalling thread is calling pthread_cond_signal() before the waiting thread calls pthread_cond_wait() . 信号线程正在等待线程调用pthread_cond_signal() 之前调用pthread_cond_wait() pthread_cond_wait() should only be called after you've tested that the invariants you are looking for aren't currently met: pthread_cond_wait()仅应在测试了当前未满足所寻找的不变量后才调用:

while (!state_) {
  result = pthread_cond_wait(&cvar_, &mutex_);
  if (result == EINVAL) ... // error handling
}

The other thing that can help sometimes is to put the signalling thread's call to pthread_cond_signal() inside the critical section. 有时可以帮助解决的另一件事是将信号线程的调用放在关键部分的pthread_cond_signal() This isn't necessary to fix your problem, but can make the program easier to reason about because you know that no-one else is holding the mutex at the time you signal them: 这不是解决您的问题所必需的,但是可以使程序更易于推理,因为您知道在发出信号时,没有人拿着互斥量:

// signalling thread
...
result = pthread_mutex_lock(&mutex_);
...
state_ = 1;
//signals the condition variable.
pthread_cond_signal(&cvar_);
result = pthread_mutex_unlock(&mutex_);
...

After reading about pthread_setcancelstate , I found that pthread_cond_wait is a cancellation point of the thread. 阅读有关pthread_setcancelstate的信息后 ,我发现pthread_cond_wait是线程的取消点。 If cancel is enabled for the thread and is deferred, then cancellation point will test for cancel. 如果取消启用了线程并被取消,则取消点将测试取消。 If any cancel is pending, thread will exit. 如果有任何取消待处理,线程将退出。

So, in my case, thread exits leaving mutex_ locked. 因此,在我的情况下,线程退出,互斥锁被锁定。 Therefore signalling thread blocks. 因此发信号通知线程块。

But there is one more doubt. 但是还有另一个疑问。 All the threads are being created from the same function using Thread class. 使用Thread类从同一函数创建所有线程。 Why only this thread has got such behaviour of cancellation? 为什么只有这个线程具有这种取消行为?

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

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