Quantcast
Channel: User Jeremy - Stack Overflow
Viewing all articles
Browse latest Browse all 46

Answer by Jeremy for Writing uint32 to uint64 is not atomic. Why?

$
0
0

As Peter Cordes points out, the atomic instructions would be required for the per-thread increments. The reason is given in the text, but the superfluous 'however' clouds it slightly:

That said, atomic instructions would be needed in cases where theper-thread counter variables were smaller than the global global_count. However, note that on a 32-bit system, the per-thread countervariables might need to be limited to 32 bits in order to sum themaccurately, but with a 64-bit global_count variable to avoid overflow.In this case, it is necessary to zero the per-thread counter variablesperiodically in order to avoid overflow. It is extremely important tonote that this zeroing cannot be delayed too long or overflow of thesmaller per-thread variables will result. This approach thereforeimposes real-time requirements on the underlying system, and in turnmust be used with extreme care.

In contrast, if all variables are thesame size, overflow of any variable is harmless because the eventualsum will be modulo the word size.

If the main thread clears the per-thread counters, it needs to do this via an atomic exchange to avoid possible data loss. If the per-thread increments do the clearing, to avoid data loss they would need some other (likely more complex) kind of interlock.


Viewing all articles
Browse latest Browse all 46

Trending Articles



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