Skip to content

synchronisation technique

sharmasadhna edited this page May 9, 2022 · 1 revision

NOTE: Both are kernel resources so obvious overheads

semaphore:

signalling mechanism: any higher priority thread can also release semaphore and take lock, thus no ownership

  1. binary semaphore : 0 (unlocked)/1 (locked)
  2. counting semaphore: 0 (unlocked)/ use and inc value, release dec value

In C++ 20 onwards

mutex:

  1. locking mechanism: lock mutex before critical section and then release,
  2. Ownership: whover locked must release the mutex, no other process can unlock

In C++ --> avaoid direct taking mutex and lock/unlock: but use

  1. std::unique_lockstd::mutex lock (mutex_var) ---> lock the mutex, and later have to be unlocked by different API's one example condition_variable (cv.wait(mutex_var)): cv automatically releases the lock here.
  2. std::lock_guardstd::mutex lock (mutex_var) --> automatically releases the mutex after the block