diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 5466995..fd69278 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -12712,11 +12712,13 @@ Flag all unnamed `lock_guard`s and `unique_lock`s. -### P.50: Define a `mutex` together with the data it guards +### P.50: Define a `mutex` together with the data it guards. Use `synchronized_value` where possible ##### Reason -It should be obvious to a reader that the data is to be guarded and how. +It should be obvious to a reader that the data is to be guarded and how. This decreases the chance of the wrong mutex being locked, or the mutex not being locked. + +Using a `synchronized_value` (see the [WG21 proposal](http://wg21.link/p0290)) ensures that the data has a mutex, and the right mutex is locked when the data is accessed. ##### Example @@ -12725,6 +12727,13 @@ It should be obvious to a reader that the data is to be guarded and how. // ... }; + class MyClass { + struct DataRecord { + // ... + }; + synchronized_value data; // Protect the data with a mutex + }; + ##### Enforcement ??? Possible?