首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >信号量如何提供互斥?

信号量如何提供互斥?
EN

Stack Overflow用户
提问于 2015-05-22 18:55:52
回答 1查看 1.2K关注 0票数 1

当信号量的值大于1时,我很难理解信号量是如何提供互斥的。

假设信号量的值最初设置为2。考虑以下两个使用相同信号量的线程:

代码语言:javascript
复制
# Thread 1
wait(s) 
critical section
signal(s)

# Thread 2
wait(s)
critical section
signal(s)

在以下情况(可能还有更多情况)中,不会提供互斥:

代码语言:javascript
复制
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?
EN

回答 1

Stack Overflow用户

发布于 2021-09-16 05:47:07

对信号量进行计数并不提供互斥。在计算信号量时,没有提供互斥,因为我们有一组需要在临界区同时执行的进程。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30394883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档