13 - Synchronization Flashcards
What are mutexes and their role in synchronization?
- Mutexes guard critical regions and implement mutual exclusion.
- Lock and unlock actions should be performed by the same task.
- They are used for simple interactions where only one task runs protected code.
What are different synchronization patterns in multi-threaded programs?
- Multi-threaded programs may require synchronization for different resource guarding and just thread execution coordination.
- Some patterns involve waiting inside a critical region for different events or conditions.
What are condition variables in Linux programming?
- Condition variables allow threads to wait inside critical regions without holding the lock, enabling other threads to enter.
- They make it possible to sleep inside a critical section and atomically release and reacquire the lock.
How does waiting inside a critical region work in thread synchronization?
- Waiting inside critical regions is crucial for long critical sections as it serializes code and affects concurrency.
- Threads wait for specific conditions or events, which is impossible with regular mutexes
Describe the operations associated with condition variables.
-
Wait()
: Atomically releases the lock and puts the thread to sleep; the lock is reacquired upon waking up. -
Signal()
: Wakes up one waiting thread, if any. -
Broadcast()
: Wakes up all waiting threads.
How do mutexes interact with condition variables in thread synchronization?
- A mutex is used alongside a condition variable to protect the critical region where threads wait.
- The wait operation on a condition variable releases and then reacquires the corresponding mutex, ensuring only one thread is in the critical region.
What are the key pthread procedures for managing condition variables?
pthread_cond_init(pthread_cond_t *cv, NULL);
pthread_cond_destroy(pthread_cond_t *cv);
pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *lock);
pthread_cond_signal(pthread_cond_t *cv);
How are condition variables created and used in pthreads?
- Condition variables are declared with
pthread_cond_t
and must be initialized. - Initialization can be static (with
PTHREAD_COND_INITIALIZER
) or dynamic (withpthread_cond_init()
). -
pthread_cond_destroy()
is used to free a condition variable when it’s no longer needed.
Explain the pthread_cond_wait
function in pthreads.
-
pthread_cond_wait()
is used when a thread wants to block and wait for a condition to be true. - The function assumes the thread has locked the mutex. It releases the mutex and blocks until awakened by
pthread_cond_signal()
from another thread. - Upon awakening, it waits to reacquire the mutex before returning.
int pthread_cond_wait(pthread_cont_t * cv, pthread_mutex_t * mux);
Explain the pthread_cond_signal
function used with condition variables.
-
pthread_cond_signal
checks for any threads waiting on the specified condition variable. - If threads are waiting, one is awakened.
- It does not guarantee the order in which threads are awakened. For waking all threads,
pthread_cond_broadcast
is used.
Can you describe the use of condition variables with an example of the producer-consumer problem?
- In the producer-consumer problem, the producer thread produces data and signals the condition variable when data is available.
- The consumer thread waits on the condition variable until data becomes available.
- Mutex locks are used in conjunction with the condition variable to protect the shared data.
What are spurious wake-ups in the context of condition variables and how are they handled?
- Spurious wake-ups occur when a thread is awakened without any thread signaling the condition variable.
- They are handled by looping around the pthread_cond_wait call and checking the condition upon wake-up.
- This ensures that the thread only proceeds if the condition is actually true.
What are read-write locks and how do they function in multi-threaded programming?
- Read-write locks allow multiple threads to read a shared resource concurrently but require exclusive access for writing.
- They prevent write starvation by not allowing new read locks if a write lock is waiting.
- This mechanism ensures efficient access for read-heavy resources while maintaining data integrity during writes.