multithreading - 多个线程可以同时等待一个信号量

标签 multithreading pthreads semaphore

多个线程可以等待单个信号量吗? 如果是,当信号量下降时,哪个信号量将被恢复?

最佳答案

作为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 schedulers SCHED_FIFO and SCHED_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/

相关文章:

java - 是否有可能使用两个锁且只有一个方法获取两个锁而所有其他方法仅获取一个锁而导致死锁?

c++ - 在 C++ <process.h> 中的头文件的 cpp 文件中启动一个线程

WPF 应用程序消息循环和 PostThreadMessage

linux - 信号量服务员命令

C <pthread.h> 将局部变量传递给线程已损坏

haskell - QSem 似乎不会阻塞线程

multithreading - 如何使用V唤醒指定的P?

java - 如何在主线程中为循环中启动的线程定义会合点?

c++ - 日志记录和多线程

c# - SemaphoreSlim 没有限制任务