Wk4L1 - Reentrant Locks and Semaphores Flashcards

1
Q

What is a Lock in multithreading?

A

A lock is a mechanism that ensures only one thread can execute a critical section at a time, providing mutual exclusion. It guarantees the “happens-before” relationship, meaning all changes made before releasing the lock are visible to the next thread acquiring it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a ReentrantLock?

A

A reentrant lock allows the same thread to acquire the lock multiple times in nested critical sections. It must be released with unlock() after the thread completes execution in the critical section.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between a ReentrantLock and a Binary Semaphore?

A

ReentrantLock: A locking mechanism where the thread that obtains the lock owns it and must release it. The same thread can hold the lock multiple times.

Binary Semaphore: A signaling mechanism that allows threads to signal and acquire resources. The thread does not “own” the semaphore, and it can be released by another thread.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the advantages of using Locks?

A
  • More flexible than synchronized methods.
  • Can be used in complex algorithms where locks are acquired and released in varying orders.
  • Support for nested critical sections with reentrant behavior.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are Semaphores?

A

Semaphores are synchronization mechanisms that control access to a finite number of resources. They use a counter to track available resources. Threads use acquire() to decrease the count and release() to increase it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a Counting Semaphore?

A

A counting semaphore allows a specified number of threads to access a resource simultaneously. The counter can be any positive value, with 0 indicating no more resources are available.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the main issue with using Locks?

A

Locks can cause performance issues since other threads are blocked or put into a waiting state, leading to expensive context switching.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does a Semaphore differ from other mutual exclusion techniques like locks?

A

Semaphores allow multiple threads to access resources at the same time (in contrast to locks that allow only one thread). They are more flexible for managing resources like thread pools or queues.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly