
Lock
Principle
- Initially free
- at most One lock holder at a time
- no holder → get lock
- release → waiter gets lock
two atomic operations
lock::acquire
- wait until lock is free
- acquire before accessing shared data
lock::release
- release lock, wake up
- release after finishing with shared data
Thread-safe

- lock을 했어도 malloc이 아직 돌아가고 있으면 p→field1 접근시 오류!
Condition Variables
Operations
- Wait()
- release lock, go to sleep, aquire lock
- 이러는 이유는 lock을 들고 있는동안(critical section) sleep할 수 없기 때문이다.
- 따라서 lock을 풀어주고 기다리는 것!
- Signal()
- Broadcast()

- remove가 먼저 acquire 성공
- queue에 아무것도 없으므로
- lock풀어주기
go to sleep
- addtoqueue가 acquire성공
- addtoqueue에서 signal
이러면 remove가 wake해 acquire
- add에서 release 후 종료
- remove에서 acquire성공 후 remove
- remove에서 release 후 return.
- add가 먼저 acquire
- queue에 넣고 signal
아무도 깨어나지 않는다
- add에서 release
- remove에서 acquire
이후 동일