|
Synchronization
- Consider a simple example where two threads on two different processors are both trying to increment a variable x at the
same time (assume x is initially 0):
THREAD 1
increment(x){
x = x+1;
}
THREAD 1
10 LOAD A, (x address)
20 ADD A, 1
30 STORE A, (x address) |
THREAD 2
increment(x){
x = x+1;
}
THREAD 2
10 LOAD A, (x address)
20 ADD A, 1
30 STORE A, (x address) |
- What are the possible outputs? After exuction of both threads resultant value of x may be 1 for some execution sequence
- To avoid situations like this the incrementation of x must be synchronized between the two threads
Synchronization is used to impose order constraints and to protect access to shared data
High Level Synchronization |
- Critical
- Atomic
- Barrier
- Ordered
|
Low Level Synchronization |
|
|