Professional Documents
Culture Documents
05 Sync19 PDF
05 Sync19 PDF
Synchronization
Lecture 5
Michael O’Boyle
1
Temporal relations
• In absence of synchronization,
– instructions executed by distinct threads must be considered
unordered / simultaneous
– Not X < X’, and not X’ < X
2
Example
Example: In the beginning...
main()
Y-axis is “time.”
A
Could be one CPU, could
pthread_create()
be multiple CPUs (cores).
foo()
B
A'
•A<B<C
B' • A' < B'
C • A < A'
• C == A'
• C == B'
3
Critical Sections / Mutual Exclusion
4
Critical sections
Critical sections
is the “happens-before” relation
T1 T2 T1 T2 T1 T2
5
When do critical sections arise?
• Shared variable:
– Globals and heap-allocated variables
– NOT local variables (which are on the stack)
6
Race conditions
• Typical symptoms
– I run it on the same data, and sometimes it prints 0 and sometimes it
prints 4
– I run it on the same data, and sometimes it prints 0 and sometimes it
crashes
7
Example: shared bank account
8
• Assume the bank’s application is multi-threaded
• A random thread is assigned a transaction when that
transaction is submitted
int withdraw(account, amount) { int withdraw(account, amount) {
int balance = get_balance(account); int balance = get_balance(account);
balance -= amount; balance -= amount;
put_balance(account, balance); put_balance(account, balance);
spit out cash; spit out cash;
} }
9
Interleaved schedules
10
Other Execution Orders
11
How About Now?
• Moral:
– Interleavings are hard to reason about
• We make lots of mistakes
• Control-flow analysis is hard for tools to get right
– Identifying critical sections and ensuring mutually exclusive access
can make things easier
12
Another example
i++; i++;
13
Correct critical section requirements
14
Mechanisms for building critical sections
• Spinlocks
– primitive, minimal semantics; used to build others
• Semaphores (and non-spinning locks)
– basic, easy to get the hang of, somewhat hard to program with
• Monitors
– higher level, requires language support, implicit operations
– easier to program with; Java “synchronized()” as an example
• Messages
– simple model of communication and synchronization based on
(atomic) transfer of data across a channel
– direct application to distributed systems
15
Locks
16
Locks: Example
Locks: Example execution
lock()
lock() Two choices:
• Spin
• Block
unlock() • (Spin-then-block)
unlock()
17
Acquire/Release
18
Using locks
acquire(lock)
balance = get_balance(account);
int withdraw(account, amount) {
balance -= amount;
acquire(lock);
balance = get_balance(account); acquire(lock)
section
critical
balance -= amount; put_balance(account, balance);
put_balance(account, balance); release(lock);
release(lock);
balance = get_balance(account);
spit out cash;
balance -= amount;
}
put_balance(account, balance);
release(lock);
spit out cash;
19
Spinlocks
20
Implementing spinlocks
21
Spinlocks: Hardware Test-and-Set
22
Implementing spinlocks using Test-and-Set
}
void acquire(lock) {
while(test_and_set(&lock->held));
}
void release(lock) {
lock->held = 0;
}
23
Reminder of use …
acquire(lock)
balance = get_balance(account);
int withdraw(account, amount) {
balance -= amount;
acquire(lock);
balance = get_balance(account); acquire(lock)
section
critical
balance -= amount; put_balance(account, balance);
put_balance(account, balance); release(lock);
release(lock);
balance = get_balance(account);
spit out cash;
balance -= amount;
}
put_balance(account, balance);
release(lock);
spit out cash;
25
Summary
26