[英]What happens if a pthread condition variable is signaled but the mutex is locked?
Consider the following: 考虑以下:
pthread_mutex_t m;
pthread_cond_t c;
//a bunch of passenger threads will all be doing this
pthread_mutex_lock(&m); //passengers join the queue for boarding one at a time
pthread_cond_wait(&c, &m); //joined queue, mutex now unlocked allowing other passengers to join queue
//wait for a ride thread to signal the variable (representing the queue), allowing passengers to board one at a time
//
//Do some work in here required for boarding the ride
//
pthread_mutex_unlock(&m); //allow the next passenger to do the work required to board
Can I guarantee that only one passenger will be able to access the "do some work here" part at a time? 我可以保证一次只能有一位乘客进入“在这里做一些工作”部分吗? From my understanding, once the condition variable is signaled, that thread will relock the mutex and proceed on.
据我了解,一旦发出条件变量信号,该线程将重新锁定互斥锁并继续执行。 What happens if after one thread gets signaled and starts doing work, the ride thread signals the cond variable again?
如果在发出一个线程信号并开始工作后,行驶线程再次向cond变量发出信号怎么办? Will the second thread wait for the first thread to unlock the mutex, or will both threads now be in the "do some work here" part?
第二个线程会等待第一个线程解锁互斥锁,还是两个线程现在都在“在这里做一些工作”部分中?
Consider if you used pthread_cond_broadcast instead of pthread_cond_signal. 考虑是否使用pthread_cond_broadcast而不是pthread_cond_signal。 All threads that are in a pthread_cond_wait on the conditions are signaled to wake up.
在该条件下处于pthread_cond_wait中的所有线程都将被唤醒。 Only one of them can because in order to exit pthread_cond_wait they each must reacquire the mutex.
它们中只有一个可以,因为每个人都必须重新获取互斥锁才能退出pthread_cond_wait。 The one that gets the mutex finishes and unlocks the mutex, and then the next one gets the mutex.
一个获取互斥锁的对象完成并解锁该互斥锁,然后下一个获取互斥锁。 Despite being a performance hit from waking more threads than needed, this should not change the correct function of code that is using of pthread_cond_wait properly.
尽管唤醒更多线程导致性能下降,但这不应正确更改使用pthread_cond_wait的代码的正确功能。
From the manpage for pthread_cond_wait: 从pthread_cond_wait的联机帮助页中:
"Spurious wakeups from the pthread_cond_timedwait() or pthread_cond_wait() functions may occur."
When a thread returns from a pthread_cond_wait it should check that there is data (that was protected by the mutex) that indicates it can actually proceed. 当线程从pthread_cond_wait返回时,应检查是否存在表明其可以继续进行的数据(受互斥锁保护)。 Typicaly this involves the thread that performed the pthread_cond_signal incrementing a count or setting a flag (protected by the mutex), and the thread that received the pthread_cond_wait looping on the the wait call while the flag value is zero, and if its not zero decrementing the flag before it leaves its mutex protected section.
通常情况下,这涉及执行pthread_cond_signal的线程增加计数或设置标志(由互斥锁保护),以及在标志值为零时在wait调用中接收到pthread_cond_wait循环的线程,如果其值不为零,则减小离开互斥保护部分之前的标志。
I don't see the expected loop, nor the signaling value addressed in the snippit you provided. 我看不到预期的循环,也看不到您提供的代码片段中提到的信令值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.