From 3663b977cdac206c1297fa5597cf5d774ab8cd9a Mon Sep 17 00:00:00 2001 From: rob100 Date: Mon, 29 Jun 2015 11:16:56 +0200 Subject: [PATCH 1/3] Added M&M rule Use mutex and mutable for member variables together (C++11) --- 07-Considering_Threadability.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/07-Considering_Threadability.md b/07-Considering_Threadability.md index 36c5cc7..66daa30 100644 --- a/07-Considering_Threadability.md +++ b/07-Considering_Threadability.md @@ -8,4 +8,11 @@ Global data leads to unintended side effects between functions and can make code ## Avoid Heap Operations -Much slower in threaded environments. In many or maybe even most cases, copying data is faster. Plus with move operations and such and things +Much slower in threaded environments. In many or maybe even most cases, copying data is faster. Plus with move operations and such and things. + +## Mutex and mutable go together (M&M rule, C++11) + +For member variables it is good practice to use mutex and mutable together. This applies in both ways: +1) A mutable member variable is presumed to be a shared variable so it should be synchronized with a mutex (or made atomic) +2) If a member variable is itself a mutex, it should be mutable. This is required to use it inside a const member function. + From 51821c8741ef2811833428a1f97d3011d07c7ee5 Mon Sep 17 00:00:00 2001 From: rob100 Date: Mon, 29 Jun 2015 11:27:21 +0200 Subject: [PATCH 2/3] Improved format --- 07-Considering_Threadability.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/07-Considering_Threadability.md b/07-Considering_Threadability.md index 66daa30..2285f05 100644 --- a/07-Considering_Threadability.md +++ b/07-Considering_Threadability.md @@ -11,8 +11,6 @@ Global data leads to unintended side effects between functions and can make code Much slower in threaded environments. In many or maybe even most cases, copying data is faster. Plus with move operations and such and things. ## Mutex and mutable go together (M&M rule, C++11) - For member variables it is good practice to use mutex and mutable together. This applies in both ways: -1) A mutable member variable is presumed to be a shared variable so it should be synchronized with a mutex (or made atomic) -2) If a member variable is itself a mutex, it should be mutable. This is required to use it inside a const member function. - +* A mutable member variable is presumed to be a shared variable so it should be synchronized with a mutex (or made atomic) +* If a member variable is itself a mutex, it should be mutable. This is required to use it inside a const member function. From 9324e48f121309c4018a02cdd216b9407f6cbe0e Mon Sep 17 00:00:00 2001 From: Robin Schlegel Date: Tue, 30 Jun 2015 19:53:33 +0200 Subject: [PATCH 3/3] Added link to article from Herb Sutter --- 07-Considering_Threadability.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/07-Considering_Threadability.md b/07-Considering_Threadability.md index 2285f05..c0d3a7e 100644 --- a/07-Considering_Threadability.md +++ b/07-Considering_Threadability.md @@ -14,3 +14,5 @@ Much slower in threaded environments. In many or maybe even most cases, copying For member variables it is good practice to use mutex and mutable together. This applies in both ways: * A mutable member variable is presumed to be a shared variable so it should be synchronized with a mutex (or made atomic) * If a member variable is itself a mutex, it should be mutable. This is required to use it inside a const member function. + +For more information see the following article from Herb Sutter: http://herbsutter.com/2013/05/24/gotw-6a-const-correctness-part-1-3/ \ No newline at end of file