L8 & L9 Flashcards
What is a barrier? What is a lock? What are both used for?
A barrier is where a number of threads have to reach to proceed.
Locks protect shared data from concurrent access.
Synchronisation
What is a use application of a barrier?
when data dependencies limit loop parallelisation
What is a way locks can be used?
To deal with race conditions by serialising access to shared data. This ensures that critical sections are executed atomically.
What are critical sections of a program?
Where shared data is accessed or updated
How do locks limit the execution of a critical section?
Serialisation: amount of threads that can concurrently execute it (generally 1)
Atomicity: when a thread A starts to run a critical section S, T must finish S before another thread can run S.
How are condition variables used in mutexes?
Condition variables are used to signal threads waiting for a lock to be free and an arbitrary event. Thread wakes up right when needed without spinning (busy wait).
pthread_cond_wait
pthread_cond_signal
What is the difference between coarse-grained locking and fine-grained locking? What is a disadvantage of each?
Coarse-grained locking involves using a single lock to protect shared data. Fine-grained locking involves using a seperate lock for each data access.
Coarse-grained locking limits parallelism. Fine-grained locking may lead to increased overhead.
What is a reentrant lock used for?
It allows a lock to be taken by a thread that already holds it without deadlocking.
What are 4 lock types?
Mutex
Reentrant lock
Semaphores
Spinlocks
Read-write locks
______ are mutexes that can be held by multiple threads.
Semaphores
In a _______, threads will be busy-wait if the lock is unavailable.
Spinlock