chapter 28 - locks Flashcards

1
Q

What is a lock used for?

A

To control access to a critical section so only one thread can enter at a time

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

What are the two states of a lock?

A

Free (unlocked) and Held (locked)

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

What is pthread_mutex_t used for?

A

It ensures mutual exclusion among threads

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

What’s the difference between coarse-grained and fine-grained locking?

A

Coarse-grained: One lock for everything → simple but limits parallelism

Fine-grained: One lock per data structure → better concurrency

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

Why can’t you build a lock using just loads and stores?

A

Because normal memory ops aren’t atomic → leads to race conditions

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

What hardware primitives help create proper locks?

A

Test-and-Set (TAS)

Compare-and-Swap (CAS)

Load-Linked / Store-Conditional (LL/SC)

Fetch-and-Add

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

What is a spin lock?

A

A lock where the thread loops (spins) until it acquires the lock

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

When are spin locks okay to use?

A

On multi-core systems with short critical sections

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

What’s the downside of spin locks on single-core systems?

A

The CPU wastes cycles spinning while the lock holder can’t even run

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

Which lock ensures fairness among threads?

A

Ticket Lock using Fetch-and-Add → FIFO order

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

What are the benefits of Compare-and-Swap (CAS)?

A

It atomically updates only if the current value matches an expected one

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

What is the LL/SC instruction pair used for?

A

To implement atomic operations and avoid cache invalidation storms

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

What can a thread do instead of spinning endlessly?

A

Call yield() to voluntarily give up the CPU

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

What’s a two-phase lock?

A

A lock that spins briefly and then sleeps if the lock isn’t acquired quickly

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

How do park() and unpark() improve locking?

A

They put threads to sleep and wake them explicitly

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

What is a futex in Linux?

A

A fast userspace mutex combining in-memory state with OS-level queues

17
Q

what does test-and-set (TAS)

A

reads and write a value to memory atomic - if value= 0, sets it to 1 and goes into critical section

18
Q

what does compare-and-swap (cas) do - what does it return

A

cas checks if a value in memory is like expected and replaces it atomic if it is true

it return the old value so you know the replacement happend

19
Q

how does load-linked/store-conditional (LL/SC) work - what does it return

A

LL reads a value and remembers the address -> SC writes it only if no one has changes that value since LL

return 1, or else 0

20
Q

how does fetch-and-add work

A

increments a counter atomically and returns the old number - the thread waits for its turn to come –> gives FIFO