Synchronization
Peterson's Algorithm
Works on uniproccessor systens.
If we have two threads, T0, T1 and a turn-variable = 0 and a lock-variable = []
If a thread wants the lock it will set the lock-variable to true. The turn-variable says hich thread is in control of the lock.
- Set lock_(tID) to true
- Set turn to 1 (set turn to the other tID)
- While (lock - tID) && turn == 1 - tID
- At this point T0 has the lock
XCHG
Spinlock
Pros
Cons
- Wastes CPU Cycles
- Bad for 1 core CPUs