|
|
|
|
|
by rocqua
3070 days ago
|
|
Yes, if your data races can pollute pointers, that makes subsequent access of those pointers undefined behavior.
But is the actual data race undefined behavior? Take the following example from the article: Thread A loads the value 0
Thread B loads the value 0
Thread A increments the value, calculating a result of 1
Thread B also calculates a result of 1
Thread A stores the value 1
Thread B stores the value 1 This is definitely indeterminate behavior, for the final value could also be 2. But if it is undefined behavior, the standard would also allow that final value to be 3 or 999 or -1 or INTMAX, it would allow anything. To see a case where this matters, suppose you want an approximate total and you do this by concurrent addition to some shared variable. You expect overlapping additions to be so infrequent as to not have a large effect on the final value.
Thus, you decide to not make that value atomic and accept the inaccuracy since it is an approximation anyway.
Maybe you do this because atomic locks an entire cache-line, and something else in the cache-line is frequently accessed due to struct outline. If the above scenario is actually 'undefined behavior' then this would be a really bad idea. You'd be at the mercy of the compiler not wanting to use this for any optimization. |
|