Spinlocks and Barrier Synchronization Flashcards

1
Q

Spinlock

A
  • Each java object has an intrinsic lock
  • So far we have dealt with threads that block
    awaiting access to a shared resource
    (This is implicit using synchronized keyword)
  • An alternative is to keep the thread active and continuously ‘spinning’ attempting to acquire the lock
     This is a spin lock
     (Potentially) improves threading performance and CPU usage
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

An Attempted Spinlock Implementation\

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

Spinlock execution

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

Spinlock Solution

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

Disabling Interrupts

A
  • Pre-emptive context switch only happens when interrupt occurs
  • Could disable interrupts to prevent context switch in critical section

While this works, comes with many disadvantages…
1. Interrupts might be disabled frequently and for a long
time
 Clock ticks, I/O events could be missed
2. Will not be sufficient when you have more than one processor
* More than once thread could be running concurrently
3. Error proneness
 Forgetting to call release_lock() means interrupts disabled forever

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

Machine Instruction

A

test-and-set, comp-and-swap, fetch-and-add

  • Atomic machine instruction
    • Sets variable passed to true,
    • Tells if variable was true or false before being set to true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Machine Instruction: test_and_set() lock

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

Software Only – Peterson’s Algorithm

A

Assumes atomic reads and writes
- Only works with two threads (can be generalized to n threads)
- Assumes thread ID are 0 and 1

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

In green and pink are instructions from
processes 0 and 1, respectively

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

Blocking vs spinning locks

A

Blocking:
- Scheduler blocks threads while they wait
- Good for long critical sections
- Frequent queue management if locks
accessed frequently

Spinning:
- Sit in a tight loop until lock acquisition
- Good for short critical sections
- Avoid queue management

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