ICLP c5 CPP STM
ICLP c5 CPP STM
programare
Software transactional memory in C++
FMI @ UNIBUC
1
Threads and synchronization in C++
Dining Problem
2
Threads and synchronization in C++
Hello threads 01-basic.cc
3
Thread Interference (data races) 02-interference.cc
Data race
• Simultaneous conflicting accesses to the same memory location
• Considered undefined behavior
• compiler can do unexpected optimizaitons
4
Solving interference using mutexes and lock_guards
03-lock_guard.cc
5
The Bank problem
Bank operations 04-account.hh
6
Concurrency anomalies at the Bank
7
Software Transactional Memory
Transactions and the ACID model
8
Software Transactional Memory
9
STM — Local (optimistic) execution
10
STM — finalizing a transaction
Commiting a transaction
• Validating the log
• each location read recorded in log must still hold the same value
• Commiting the log
• commits all writes from the log to the memory (atomically)
Retrying a transaction
• If validation fails . . .
• the transaction has an inconsistent view of the memory
• . . . then abort transaction, reinitialize log, and retry
11
STM in C++-17
12
Dining Problem
Dining Philosophers Problem
13
Solution 1 09-philosophers.cc
14
Solution 2 10-philosophers-locks.cc
15
Solution 3 (do it yourselves)
16