问题描述
我米使用调用pthread_cond_wait(安培; cond_t,&安培;互斥);
在我的计划,我米奇怪,为什么这个功能需要作为第二个参数互斥变量
I m using pthread_cond_wait(&cond_t, &mutex);
in my program and I m wondering why this function need as a second parameter a mutex variable.
请问调用pthread_cond_wait()
解锁在beggining互斥(执行的beggining 调用pthread_cond_wait()
)和那么当它完成锁定(只是临走前调用pthread_cond_wait()
)?
Does the pthread_cond_wait()
unlock the mutex at the beggining (beggining of the execution pthread_cond_wait()
) and then locked when it finish (just before leaving pthread_cond_wait()
)?
推荐答案
在第一个线程调用它释放互斥锁,并等待,直到条件 cond_t
的信号作为完整的和 互斥
是可用的。
When the first thread calls pthread_cond_wait(&cond_t, &mutex);
it releases the mutex and it waits till condition cond_t
is signaled as complete and mutex
is available.
所以,当是所谓的其他螺纹,它并不唤醒该等待尚未螺纹。 互斥
必须先解锁,只有有一个机会,第一个线程将获得锁,这意味着的后的互斥应已被锁定,并由调用线程拥有。
So when pthread_cond_signal
is called in the other thread, it doesn't "wake up" the thread that waits yet. mutex
must be unlocked first, only then there is a chance that first thread will get a lock, which means that "upon successful return of pthread_cond_wait
mutex shall have been locked and shall be owned by the calling thread."
这篇关于是否调用pthread_cond_wait(安培; cond_t,&安培;互斥);解锁,然后锁定互斥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!