From 1a9a35d2d93564bd5480a2c89cb225f6b58980d5 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Thu, 2 May 2019 13:05:35 -0500 Subject: [PATCH] Tighten up CP.1 (#1405) * Tighten up CP.1 * balanced verb usage in first sentence * changed third sentence to "libraries not using threads", as I believe this was the original author's intended meaning. * clarified "this" in fourth sentence * cut wordiness of "thanks to the magic of cut-and-paste", as it added no value * changed "Example" heading to "Example, bad" * added "bad:" comment above statics in the example * added an explanatory sentence immediately after the example * changed "works perfectly in a single-threaded" after example to "works as intended in a single threaded". Also balanced the structure of the two comma separated phrases inside this sentence. * strengthened parenthetical explanation in second bullet of "could be made safe" section * Correct grammar mistake pointed out by @cubbimew * Remove specific cache details in CP.1 per @hsutter's request --- CppCoreGuidelines.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) 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.