Locks & Race Conditions Flashcards

1
Q

Synchronization in Share Memory

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

Shared Memory Synchronization
(Critical Section)

A

Region of code that accesses variables that are shared between threads.

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

Shared Memory Synchronization
(Shared Resource)

A

can be anything of interest: bank balance, data structure, device, network connection. Always represented in memory, so we can talk equivalently in terms of Shared Variables.

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

Example: Banking

A

Two threads called update on the same bank account. The code inside update is the critical region, the shared variable being bal.

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

Problem: Banking

A

Final result may be that the balance is 5 instead of 10
This is a lost update problem.

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

Problem Source

A

update of bank account is not atomic, meaning that when one thread is in the middle of updating a bank account, another thread may start updating the same account

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

Problem Nature: Race Condition

A

The condition where an incorrect program output may be generated depending on the relative order in which instructions from multiple threads are interleaved (executed).

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

Threads Introduce Nondeterminism

A
  • Sequential programs are deterministic: for the same input, the same output
  • Threads make a program nondeterministic:
    • For the same input, output depends on the interleaving of instructions from different threads. Race condition if output can be incorrect.
  • Aim of synchronization: Eliminate race conditions
  • Correct output? One influential idea is that any output from interleaving multiple threads is the same as an output from executing the threads sequentially.
  • Atomicity of a critical section means that only one thread may be in it and the thread must finish the section before another thread may enter, thus ensuring “sequentiality.”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Locks: Solving Banking Problem via Mutual Exclusion

A

Mutual exclusion for a resource: limits resource access to one thread at a time. Guarantees atomicity.

Lock can be in one of two states
– Held: A thread is in the critical section (no other thread can enter)
– Not held: No thread in the critical section (any thread can enter)

Two operations to move lock between states
– Acquire: mark lock as held, or wait until release
– Release: mark as not held

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

lock_example()

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

Lock Declaration

A

Declared like a variable
– Lock L;
Programs can have multiple locks
– Lock L, P;
– Call acquire at start of critical section
– Call release at end of critical section
– Achieves mutual exclusion and progress

Acquisition of a lock blocks only threads attempting to acquire the same lock.

Must use same lock for all critical sections accessing the same resource

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

Mutual Exclusion in Java

A

Every Java object has an implicit (i.e. invisible) lock

Lock is achieved via the synchronized block
– Prevents thread entry to block until lock acquisition
– Lock release on block completion so other threads enter

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

Locks in Java - Method

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

Locks in Java – Code Block

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

Bank_account in action

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

Terminology Recap

A

Critical section
– Logically, all the code that accesses a shared variable (resource).

Race condition
– Condition where incorrect program output may be generated depending on the interleaving of instructions from multiple threads.

Atomic code
– Code that is executed by a thread indivisibly from the point of view of other threads trying to execute the same code.
– Idea: To avoid race conditions, make critical sections atomic.

Mutual exclusion
– Only one thread may access a shared resource at a time.
– Ensures atomicity

Locks
– A mechanism for guaranteeing mutual exclusion.