Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Edit. An edit to the question means most of this isn't relevant. Still, I'll leave this as all the concerns in the C# case also carry to the C++ case, but the C++ case brings many more concerns as stated, so it's not entirely irrelevant.</p> <hr> <p>Yes, but...</p> <p>Assuming you mean that this CAS is atomic (which is the case with C# <code>Interlocked.CompareExchange</code> and with some things available to use in some C++ libraries) the it's thread-safe in and of itself.</p> <p>However <code>DestVar = Value</code> could be thread-safe in and of itself too (it will be in C#, whether it is in C++ or not is implementation dependent).</p> <p>In C# a write to an integer is guaranteed to be atomic. As such, doing <code>DestVar = Value</code> will not fail due to something happening in another thread. It's "thread-safe".</p> <p>In C++ there are no such guarantees, but there are on some processors (in fact, let's just drop C++ for now, there's enough complexity when it comes to the stronger guarantees of C#, and C++ has all of those complexities and more when it comes to these sort of issues).</p> <p>Now, the use of atomic CAS operations in themselves will always be "thead-safe", but this is not where the complexity of thread safety comes in. It's the thread-safety of combinations of operations that is important.</p> <p>In your code, at each loop either the value will be atomically over-written, or it won't. In the case where it won't it'll try again and keep going until it does. It could end up spinning for a while, but it will eventually work.</p> <p>And in doing so it will have exactly the same effect as simple assignment - including the possibility of messing with what's happening in another thread and causing a serious thread-related bug.</p> <p>Take a look, for comparison, with the answer at <a href="https://stackoverflow.com/questions/3870713/is-this-use-of-a-static-queue-thread-safe/3871198#3871198">Is this use of a static queue thread-safe?</a> and the explanation of how it works. Note that in each case a CAS is either allowed to fail because its failure means another thread has done something "useful" or when it's checked for success more is done than just stopping the loop. It's combinations of CASs that each pay attention to the possible state caused by other operations that allow for lock-free wait-free code that is thread-safe.</p> <p>And now we've done with that, note also that you couldn't port that directly to C++ (it depends on garbage collection to make some possible ABA scenarios of little consequence, with C++ there are situations where there could be memory leaks). It really does also matter which language you are talking about.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload