Lock and Condition Variables


Recall the problem from intro-to-os


What is shared/not shared in a multithreaded program?


Key definitions


Requirements for Critical Section Handling


Lock: Definition


Semantic of locking: lock() and unlock()


How do we build a lock?


Criteria of a good lock


First attempt: controlling interrupts


We need CPU hardware support


Spin lock with Test-and-Set


Spin lock with Test-and-Set: how does it work?


Evaluating the basic spin lock


Scenario: one thread waiting on another


A lock-based solution


How to wait for a condition?


Lock and conditional variable


Do we really need both?


The producer/consumer (bounded buffer) problem


Vanilla: not thread-safe


First implementation


First implementation: broken


First implementation: why?


Second implementation: slightly less broken


Third implementation: finally work


A generalized solution