Quantcast
Viewing latest article 2
Browse Latest Browse All 5

Answer by sehe for Is struct assignment atomic in C/C++?

No it isn't.

That is actually a property of the CPU architecture in relation to the memory layout of struck

You could use the 'atomic pointer swap' solution, which can be made atomic, and could be used in a lockfree scenario.Be sure to mark the respective shared pointer (variables) as volatile if it is important that changes are seen by other threads 'immediately'This, in real life (TM) is not enough to guarantee correct treatment by the compiler. Instead program against atomic primitives/intrinsics directly when you want to have lockfree semantics. (see comments and linked articles for background)

Of course, inversely, you'll have to make sure you take a deep copy at the relevant times in order to do processing on the reading side of this.

Now all of this quickly becomes highly complex in relation to memory management and I suggest you scrutinize your design and ask yourself seriously whether all the (perceived?) performance benefits justify the risk. Why don't you opt for a simple (reader/writer) lock, or get your hands on a fancy shared pointer implementation that is threadsafe ?


Viewing latest article 2
Browse Latest Browse All 5

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>