条件变量信令问题 [英] Condition variable signalling issue
问题描述
我在喝汤。这个主意可能不好,但是我确实需要解决方案。
I am in a soup. The idea may be bad but i do need a solution.
-
我有两个条件变量,比如A和B。
I have two condition variable, say A and B.
线程1、2和3正在等待A。线程4正在等待B。
Threads 1, 2 and 3 are waiting on A. Thread 4 is waiting on B.
B将被线程2设为pthread_cond-signal(),即线程4将被线程2唤醒唤醒
。
B will be pthread_cond-signal() by thread 2, that is thread 4 will be signaled to wake up by thread 2.
现在,我有另一个线程5,它在条件变量A上为pthread_cond_broadcasts()。在线程4唤醒之前,我需要唤醒所有线程1、2和3。也就是说,如果线程2唤醒并且B线程4上的信号可能在线程3之前唤醒,这不是我想要的。
Now, I have another thread 5 which pthread_cond_broadcasts() on condition variable A. I need all threads 1, 2 and 3 to wake up before thread 4 wakes up. That is say if thread 2 wakes up and signals on B thread 4 may wake up before thread 3 does, which is not what i want.
任何指针都将受到高度赞赏。
Any pointers will be highly appreciated.
谢谢
推荐答案
使用信号量:让线程1-3中的每一个都张贴该信号量,并让线程4等待该信号量3次而不是
Use a semaphore: have each of threads 1-3 post the semaphore, and have thread 4 wait on the semaphore 3 times instead of on a condition variable.
您将要使用 sem_init(3)
或 sem_open(3)
创建信号灯, sem_post(3)
发布信号量, sem_wait(3)
等待信号量,然后 sem_destroy(3)
(如果使用 sem_init
创建)或 sem_close(3)
和 sem_unlink(3)
(如果使用<$创建c $ c> sem_open )销毁信号量。
You'll want to use sem_init(3)
or sem_open(3)
to create the semaphore, sem_post(3)
to post the semaphore, sem_wait(3)
to wait on the semaphore, and then either sem_destroy(3)
(if created with sem_init
) or sem_close(3)
and sem_unlink(3)
(if created with sem_open
) to destroy the semaphore.
这篇关于条件变量信令问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!