diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 06cce5b..5b57554 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -13797,16 +13797,17 @@ Concurrency and parallelism rule summary: ##### Reason -It is hard to be certain that concurrency isn't used now or will be sometime in the future. +It's hard to be certain that concurrency isn't used now or won't be used sometime in the future. Code gets reused. -Libraries using threads may be used from some other part of the program. -Note that this applies most urgently to library code and least urgently to stand-alone applications. -However, thanks to the magic of cut-and-paste, code fragments can turn up in unexpected places. +Libraries not using threads may be used from some other part of a program that does use threads. +Note that this rule applies most urgently to library code and least urgently to stand-alone applications. +However, over time, code fragments can turn up in unexpected places. -##### Example +##### Example, bad double cached_computation(double x) { + // bad: these two statics cause data races in multi-threaded usage static double cached_x = 0.0; static double cached_result = COMPUTATION_OF_ZERO; double result; @@ -13819,8 +13820,6 @@ However, thanks to the magic of cut-and-paste, code fragments can turn up in une return result; } -Although `cached_computation` works perfectly in a single-threaded environment, in a multi-threaded environment the two `static` variables result in data races and thus undefined behavior. - There are several ways that this example could be made safe for a multi-threaded environment: * Delegate concurrency concerns upwards to the caller.