A lock-based data structure uses mutexes and locks to control concurrent access. Readers may share access while writers require exclusive access; operations can also allow different kinds of work concurrently while serializing identical operations. section 6.1.1
Designing one means balancing safe access with opportunities for concurrency:
- Lock the right mutex whenever data is accessed, and hold it for as little time as possible. section 6.2
- Keep data inaccessible outside its lock and prevent races in the interface. section 6.2
- Using separate mutexes for different parts can increase concurrency, but operations needing multiple locks introduce deadlock risks. section 6.2
A stack with one mutex provides a simple starting point for considering both thread safety and how much concurrency a design permits. Lock-free designs are treated separately. section 6.2 section 6.1.1
