Talk:cpp/named req/Mutex
From cppreference.com
[edit] single total order
The text states:
- "All lock and unlock operations on a single mutex occur in a single total order"
What does this mean?
Does this mean, that if thread_0 locked the mutex; and then thread_1, thread_2, thread_3, etc. call lock() on it in this order, will they obtain the lock on the mutex in this same order??
Run this code
#include <iostream> #include <mutex> #include <thread> #include <atomic> #include <functional> #include <cassert> #include <vector> std::mutex mut; unsigned cnt; std::atomic<bool> wait; void func(unsigned i) { std::lock_guard<std::mutex> lock(mut); while (wait) { std::this_thread::sleep_for(std::chrono::milliseconds{10}); } if (i != cnt) {