Any real-time scenarios explaining on each would be appreciated. Is there any other way to handle synchronization apart from these in pthreads. How do mutex differ from recursive-mutexes(any real-time scenario)?
What are the differences among mutex, semaphore and read write locks
11.7k Views Asked by user2601350 AtThere are 2 best solutions below

A mutex can be used to protect a shared resource (a variable, a file, a peripheral device) from modifications that could leave it in an inconsistent state.
A semaphore can be used to manage a finite pool of identical shared resources (one important case of this is an IPC queue). Threads can take a resource from the pool, put one to the pool, or wait until one becomes available. Note you may still have to use a mutex in addition to a semaphore (to protect the pool data structure itself).
A read-write lock can be used to protect a shared resource that can be either read or written to (modified). All reader threads can access it simultaneously. A writer thread needs exclusive access.
A conditional variable can be used, together with a mutex, to signal events.
Wikipedia can be used to read about most of this.
This is very plainly answered in the
boost::interprocess
docsand
boost interprocess doesn't explicitly have things called reader-writer locks, however it does implement them using
shared_locks
, and aupgrade_lock
andupgrade_to_unique lock
C++ itself does not yet have
reader-writer locks
(using shared mutexes) but @Howard Hinnet did try to get them in there, if you look here he also provides the codeC++ does not have semaphores, and mutexes are only in the new C++11 standard as far as a I know, which leads to why most of this was about
boost::interprocess