绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
Differen cebetween Mutex
2019-09-09 16:36:31

They are NOT the same thing. They are used for different purposes!

While both * of semaphores have a full/empty state and use the same API, their usage is very different.

Mutual Exclusion Semaphores

Mutual Exclusion semaphores are used to protect shared resources (data structure, file, etc..).

A Mutex semaphore is "owned" by the task that takes it. If Task B attempts to semGive a mutex currently held by Task A, Task B's call will return an error and fail.

Mutexes always use the following sequence:

  - SemTake

  - Critical Section

  - SemGive

Here is a simple example:

  Thread A                     Thread B

   Take Mutex

     access data

     ...                        Take Mutex  <== Will block

     ...

   Give Mutex                     access data  <== Unblocks

                                  ...

                                Give Mutex

Binary Semaphore

Binary Semaphore address a totally different question:

Task B is pended waiting for something to happen (a sensor being tripped for example).

Sensor Trips and an Interrupt Service Routine runs. It needs to notify a task of the trip.

Task B should run and take appropriate actions for the sensor trip. Then go back to waiting.

   Task A                      Task B

   ...                         Take BinSemaphore   <== wait for something

   Do Something Noteworthy

   Give BinSemaphore           do something    <== unblocks

Note that with a binary semaphore, it is OK for B to take the semaphore and A to give it.

Again, a binary semaphore is NOT protecting a resource from access. The act of Giving and Taking a semaphore are fundamentally decoupled.

It typically makes little sense for the same task to so a give and a take on the same binary semaphore.

分享好友

分享这个小栈给你的朋友们,一起进步吧。

应用开发
创建时间:2020-06-17 15:31:04
应用软件开发是指使用程序语言C#、java、 c++、vb等语言编写,主要是用于商业、生活应用的软件的开发。
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

技术专家

查看更多
  • 栈栈
    专家
戳我,来吐槽~