notes

Personal notes
git clone git://git.laack.co/notes.git
Log | Files | Refs

Mutex.md (1264B)


      1 # Mutex
      2 
      3 **Source:** CS 6200
      4 
      5 **Chapter:** P2L2
      6 
      7 **Definition:** A mutex is a mutual exclusion mechanism that only allows one thread to perform an operation on some data at a given time.
      8 
      9 ## Specifics
     10 
     11 Operating Systems and threading libraries support mutexes, which work as a lock.
     12 
     13 A common data structure to represent mutexes is locked (boolean), the owner, and blocked threads (not necessarily ordered). When a thread tries to acquire a lock it will be blocked until the lock is released by the owner. Additionally, a thread must unlock the lock after exiting the critical section otherwise all waiting threads will be blocked indefinitely.
     14 
     15 The code section protected by the lock is called the **critical section**. This code should correspond to code can only safely be executed by one thread at a time. 
     16 
     17 ## Adaptive Mutexes
     18 
     19 On multi-core systems (and only on multi-core systems), it can sometimes makes sense to spin instead of blocking when trying to acquire a mutex. This is because the cost of context switching and adding a thread to a mutex queue can sometimes be greater than burning a few cycles spinning.
     20 
     21 This motivates adaptive mutexes, which adapt the blocking / spinning behavior contextually, based on what is expected to be most efficient.