Reading various posts on SO on differences between the two (mutex and semaphore) I have come to the following conclusion please correct me if I am wrong.This is mostly related to windows. I understand that critical sections are sections in a code that need to be protected (i.e) cannot be accessed by multiple threads at the same time. Now in order to protect those critical sections Mutexes are used. These mutexes can be either algorithms or data structures. Now mutexes can generally be in two flavours (intra process and inter process) . For intra process in which no calls to the kernel for locking are made we could use Boost Thread synchronization primitives such as lock_guard
, unique_lock
, shared_lock
(single writer/multiple readers) and for inter-process we could use Boost Interprocess semaphore.Now these inter-process mutexes are basically called semaphore. The reason I concluded that
was because of this post which states
Semaphore is signaling mechanism (βI am done, you can carry onβ kind of signal). For example, if you are listening songs (assume it as one task) on your mobile and at the same time your friend called you, an interrupt will be triggered upon which an interrupt service routine (ISR) will signal the call processing task to wakeup.
Now Boost interprocess states
.. Boost.Interprocess implements similar mechanisms to synchronize threads from different processes.
Please let me know if my understanding of semaphore is in the correct direction.
Now another definition of semaphore which I dont understand comes from here the selected answer states
A semaphore does the same as a mutex but allows x number of threads to enter.
Which correctly describes what a semaphore does ? Does it allow interprocess resource protection or does it allow a specific number of threads to access a resource ? If it does the second one wouldn't it corrupt the resource since multiple threads are accessing it.