Written below is a collection of my miscellaneous and scattered notes representing what I learned from this week:
- synchronization primitive is a general term that encompasses all tools that help coordinate the execution of multiple threads or processes, so they don't interfere with each other
- Examples include:
- Mutex
- Reader-Writer lock
- Semaphore
- Condition variables
- condition variables are essentially flags that allow a thread to either sleep or be woken up when a certain condition becomes true. This is done to avoid phenomenon called inefficient spinning.
- we declare a condition variable by calling "pthread_cond_t"
- pthread_cond_wait() a thread will sleep when this function is called
- pthread_cond_signal() a thread will wake when this function is called
- semaphore is an object that holds an integer value. This integer value is affected by 2 main function operations:
- sem_wait(), this will decrement the value
- sem_post(), this will increment the value
- the integer value serves as a counter of available permissions.
- semaphores must first be initialized with a value. This value determines the semaphore's behavior
- 1 means it will behave as a binary semaphore (essentially act as a mutex lock)
- greater than 1 means it behaves like a counting semaphore (allowing N resources at once)
- 0 means all waiters will block until sem_post() is called
- a semaphore is essentially just a more flexible form of synchronization primitive that can act as mutex or as a condition variable
- reader-writer lock is a type of lock (just like mutex is) except it allows:
- multiple readers to hold the lock at the same time OR
- just one writer will exclusively hold onto it
No comments:
Post a Comment