mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 12:44:42 +03:00
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
This commit is contained in:
@@ -13797,16 +13797,17 @@ Concurrency and parallelism rule summary:
|
|||||||
|
|
||||||
##### Reason
|
##### 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.
|
Code gets reused.
|
||||||
Libraries using threads may be used from some other part of the program.
|
Libraries not using threads may be used from some other part of a program that does use threads.
|
||||||
Note that this applies most urgently to library code and least urgently to stand-alone applications.
|
Note that this rule 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.
|
However, over time, code fragments can turn up in unexpected places.
|
||||||
|
|
||||||
##### Example
|
##### Example, bad
|
||||||
|
|
||||||
double cached_computation(double x)
|
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_x = 0.0;
|
||||||
static double cached_result = COMPUTATION_OF_ZERO;
|
static double cached_result = COMPUTATION_OF_ZERO;
|
||||||
double result;
|
double result;
|
||||||
@@ -13819,8 +13820,6 @@ However, thanks to the magic of cut-and-paste, code fragments can turn up in une
|
|||||||
return result;
|
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:
|
There are several ways that this example could be made safe for a multi-threaded environment:
|
||||||
|
|
||||||
* Delegate concurrency concerns upwards to the caller.
|
* Delegate concurrency concerns upwards to the caller.
|
||||||
|
|||||||
Reference in New Issue
Block a user