多线程 – 信号量P和V操作是否为原子?

前端之家收集整理的这篇文章主要介绍了多线程 – 信号量P和V操作是否为原子?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
可以在信号量上执行的P()和V()操作是否保证原子?信号量可以防止两个进程进入P()?

解决方法

假设我们有一个二进制信号量s,其值为1,两个进程同时尝试执行P on.只有其中一个操作才能在下一个V操作之前完成;尝试执行P操作的其他进程被暂停.

取自我的大学笔记:

We can think if P and V as controlling
access to a resource:

When a process wants to use the
resource,it performs a P operation:
if this succeeds,it decrements the
amount of resource available and the
process continues; if all the
resource is currently in use,the
process has to wait.

When a process is finished with the
resource,it performs a V operation:
if there were processes waiting on the
resource,one of these is woken up;
if there were no waiting processes,
the semaphore is incremented
indicating that there is now more of
the resource free. Note that the
definition of V doesn’t specify which
process is woken up if more than one
process has been suspended on the same
semaphore.

信号量可以解决互斥和条件同步问题.所以你的问题的答案是:是的.

猜你在找的Java相关文章