简体   繁体   English

如果发出了pthread条件变量但互斥锁已锁定的情况,会发生什么情况?

[英]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.

相关问题 如果在调用pthread_cond_wait()之后,另一个线程获取被锁定的互斥锁然后调用phread_cond_broadcast会发生什么? - What happens if after calling pthread_cond_wait(), another thread acquires the mutex that was locked and then phread_cond_broadcast is called? 当pthread在等待互斥锁时死机会发生什么? - What happens when a pthread dies while waiting on a mutex? 如果我在使用 PTHREAD_PROCESS_SHARED 时不调用 pthread_mutex_destroy 会发生什么 - What happens if I do not call pthread_mutex_destroy when using PTHREAD_PROCESS_SHARED C对如何初始化和实现pthread互斥和条件变量感到困惑 - C Confused on how to initialize and implement a pthread mutex and condition variable 尝试获取pthread_mutex_lock(&mutex)的线程如果他们没有获得锁定会发生什么? - Threads trying to acquire pthread_mutex_lock(&mutex) What happens if they don't get the lock? 当线程在已解锁的互斥锁上调用 pthread_mutex_unlock 时会发生什么 - What happens when a thread calls pthread_mutex_unlock on an already unlocked mutex 在进程之间共享条件变量和互斥锁:互斥锁之前是否必须锁定? - Share condition variable & mutex between processes: does mutex have to locked before? 当 pthread_cond_broadcast 被调用并且多个线程被唤醒只是为了争夺同一个互斥锁时会发生什么? - What happens when pthread_cond_broadcast is called and multiple threads are awoken only to compete for the same mutex? 被 pthread_cond_signal() 唤醒但失去互斥锁竞争的线程会发生什么 - What happens to a thread that got woken up by pthread_cond_signal() but lost competition for a mutex 如果在调用pthread_mutex_lock()时线程未成功执行该怎么办? - What happens if a thread doesn't succeed when making a pthread_mutex_lock() call?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM