From 9180af5306432e4a735e2f36c9a2b3252efff0ae Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 29 Jul 2015 19:49:04 -0600 Subject: [PATCH] Update 07-Considering_Threadability.md --- 07-Considering_Threadability.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/07-Considering_Threadability.md b/07-Considering_Threadability.md index e5e19b8..5c1f695 100644 --- a/07-Considering_Threadability.md +++ b/07-Considering_Threadability.md @@ -2,10 +2,20 @@ ## Avoid Global Data -This includes statics and singletons. - Global data leads to unintended side effects between functions and can make code difficult or impossible to parallelize. Even if the code is not intended today for parallelization, there is no reason to make it impossible for the future. +### Statics + +Besides being global data, statics are not always constructed and deconstructed as you would expect. This is particularly true in cross-platform environments. See for example, [this g++ bug](9https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66830) regarding the order of destruction of shared static data loaded from dynamic modules. + +### Shared Pointers + +`std::shared_ptr` is "as good as a global" (http://stackoverflow.com/a/18803611/29975) because it allows multiple pieces of code to interact with the same data. + +### Singletons + +A singleton is often implemented with a static and/or `shared_ptr` + ## 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.