13 - Synchronization Flashcards

1
Q

What are mutexes and their role in synchronization?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are different synchronization patterns in multi-threaded programs?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are condition variables in Linux programming?

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does waiting inside a critical region work in thread synchronization?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Describe the operations associated with condition variables.

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do mutexes interact with condition variables in thread synchronization?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the key pthread procedures for managing condition variables?

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How are condition variables created and used in pthreads?

A
  • Condition variables are declared with pthread_cond_t and must be initialized.
  • Initialization can be static (with PTHREAD_COND_INITIALIZER) or dynamic (with pthread_cond_init()).
  • pthread_cond_destroy() is used to free a condition variable when it’s no longer needed.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain the pthread_cond_wait function in pthreads.

A
  • 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);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain the pthread_cond_signal function used with condition variables.

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can you describe the use of condition variables with an example of the producer-consumer problem?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are spurious wake-ups in the context of condition variables and how are they handled?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are read-write locks and how do they function in multi-threaded programming?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly