多个线程可以等待单个信号量吗? 如果是,当信号量下降时,哪个信号量将被恢复?
最佳答案
作为document对于 sem_post
建议,
If the value of the semaphore resulting from this operation is zero, then one of the threads blocked waiting for the semaphore will be allowed to return successfully from its call to
sem_wait()
. If the symbol_POSIX_PRIORITY_SCHEDULING
is defined, the thread to be unblocked will be chosen in a manner appropriate to the scheduling policies and parameters in effect for the blocked threads. In the case of the schedulersSCHED_FIFO
andSCHED_RR
, the highest priority waiting thread will be unblocked, and if there is more than one highest priority thread blocked waiting for the semaphore, then the highest priority thread that has been waiting the longest will be unblocked. If the symbol_POSIX_PRIORITY_SCHEDULING
is not defined, the choice of a thread to unblock is unspecified.
此外,如 sem_wait
在应用程序使用部分中定义,可能存在涉及具有不同优先级级别的线程的优先级反转的情况
关于multithreading - 多个线程可以同时等待一个信号量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16163932/