当信号量的值大于1时,我很难理解信号量是如何提供互斥的。
假设信号量的值最初设置为2。考虑以下两个使用相同信号量的线程:
# Thread 1
wait(s)
critical section
signal(s)
# Thread 2
wait(s)
critical section
signal(s)在以下情况(可能还有更多情况)中,不会提供互斥:
1. Thread 1 is executing, it calls wait(s) and the value of s is decremented to one.
2. There is a context switch, Thread 2 is executing, it calls wait(s) and the value of s is now 0
3. Neither Thread 1 or Thread 2 are suspended since s>0 in both cases.
4. If any context switch happens from now on, both the threads will be executing their critical section
Doesn't this violate the principle of mutual exclusion?发布于 2021-09-16 05:47:07
对信号量进行计数并不提供互斥。在计算信号量时,没有提供互斥,因为我们有一组需要在临界区同时执行的进程。
https://stackoverflow.com/questions/30394883
复制相似问题