May 2017
Intermediate to advanced
590 pages
17h 18m
English
You need to be familiar with the various mutexes and locks available in the standard library. I recommend that you read the previous recipe, Synchronizing access to shared data with mutex and locks, to get an overview of them.
The purpose of this recipe is to transform the following class so we can avoid using std::recursive_mutex:
class foo_rec { std::recursive_mutex m; int data; public: foo_rec(int const d = 0) : data(d) {} void update(int const d) { std::lock_guard<std::recursive_mutex> lock(m); data = d; } int update_with_return(int const d) { std::lock_guard<std::recursive_mutex> lock(m); auto temp = data; update(d); return temp; } };
Read now
Unlock full access