当信号量的值大于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?