Conc ds
- Multiple thread concurrently access
- Same/distinct ops
- Self consistent view
Thread safe!!
- no data loss/corruption
- invariants
- no RC
mutex
lose true concurrent access
Serialization: performing actual access becomes serialized
How to do true concurrency?
making invariants hold
- often broken during DS update (e.g. no. of items containing the item list count)
- broken invariants SHOULD NOT BE VISIBLE from the outside.
Use invariants to reason about program correctness.
e.g. Node deletion from linked list
one thread breaks the linked list.
however other threads SHOULD NOT SEE this breakage!
Thread safety of DS
- Ensure no thread see invariants broken by a particular thread
- Avoid race conditions by providing complete operations
- double free
- use after free
- data race
- Behavious in presence of exception
- Minimize deadlock when using DS by using properly scoped locks
- Lock at the appropriate granularity
- constructor and destructor require exclusive access to the data structure.
- if you have assignment, swap(), copy constructor.
- can they be called at the same time?
- Don’t break up ops to prevent interleaving.
shared_mutex
: can have different level of mutex access (shared and exclusive)
- can solve readers-writers problem
notify_all
notify_one
make sure that the constructor for an object being used has finished before you start using the object.
Designing for concurrency:
Try to protect the SMALLEST POSSIBLE REGION of the code.