Lecture 8: Synchronisation in Parallel Programming - Locks and Barriers, Condition Variables, Lecture 9: More about locks Flashcards

1
Q

What are barriers, locks and condition variables used for?

A

Barriers: simple mechanism letting threads wait for each other (pthread barrier)

Locks: allowing threads to safely access shared data (prevent concurrent access)

Condition variables: for event signalling between threads

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

When are barriers used?

A
  1. Want the whole answer from this step before proceeding to the next step
  2. Would also use when data dependence limits loop parallelisation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the term for

Concurrent operations on shared state should not happen at the same time

A

Race condition

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

What is the term for

Bits of code in our program where shared data is accessed/updated

A

Critical sections

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

What do locks ensure?

A

Serialisation: amount of threads that can concurrently execute it (generally 1)
Atomicity: when a thread T starts to run a critical section S, T must first finish S before another thread can enter S

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

What is the term for

Synchronisation primitive enforcing limits on the execution of a critical section

A

Lock

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

True or False

Multiple threads can hold a given lock at a time

A

False

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

What is a mutex?

A

Mutual exclusion lock

pthread_mutex

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

What is an example of lock usage? Why is it important?

A

Bounded buffer: circular FIFO producer-consumer buffer.

If locks are not used:
1. Could have two threads writing (deposit) to or reading (extract) from the same element of buffer, and one value is lost. 2. Could then either increment in_index
Once: whole call of deposit() lost
Twice: spurious (old) value apparently deposited

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

Benefits and drawbacks of usleep?

A

With the usleep set to an arbitrary time, may sleep for a much longer time than needed

Without usleep, keep trying non-stop, monopolising the CPU and wasting cycles

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

What are condition variables?

A

Used to signal threads (event signalling) waiting for a conjunction of:
1. A lock becoming free and
2. An arbitrary event (e.g. buffer becoming non-full)

pthread_cond_wait, pthread_cond_signal

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

What are some issues with locks?

A
  1. Deadlock
  2. Lost wakeup issue
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can lost wakeup occur? How can it be solved?

A

Implementation details, timing, or race conditions in the signaling mechanism

Wake up all threads (vs. a single thread) waiting on a condition variable

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

Why is it important to get your synchronisation strategy right from the beginning?

A

Concurrency issues are hard to debug

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

What determines how big a chunk of code which depends on obtaining a lock should you write?

A

Granularity

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

What are the types of granularity?

A
  1. Fine-grained: divides the shared resource or data structure into multiple smaller segments, and each segment is protected by its own lock
  2. Coars-grained: applying a single lock to encompass larger sections of code or the entire shared resource
17
Q

A ____ lock can be taken by a thread that already holds it

A

reentrant

18
Q

What is a rentrant lock used for?

A

A thread locking a lock it already holds results in undefined behaviour, so using a renentrant lock avoids a thread deadlocking with itself

19
Q

Match the following

  1. Semaphores
  2. Spinlocks
  3. Read-write locks

A. Threads attempting to hold an unavailable lock will busy-wait, instead of sleep (mutexes, monopolises CPU, lower wakeup latency)
B. Mutexes that can be hold by multiple threads, useful to coordinate access to a fixed number of resources
C. Allows concurrent reads and exclusive writes

A
  1. B
  2. A
  3. C