Ordinary reading atomic managed variable. Is it correct?

Sorry for probably simple question.

Driver has variable and manages its changes via atomic operations: InterlockedIncrement, InterlockedDecrement etc.
I guessed that reading this variable also asks atomic operation, such as InterlockedOr(&MyCounter,0).
But in this Driver I see simple reading MyCounter!?!

Is it correct?
Or if variable is managed as atomic, all access has to be via atomic?

I want via MSDN and did not find direct answer on this question.

Thanks,
MG.

Atomicity is required when you want to modify a value… which requires reading it, updating it, and then writing it back to storage. When these steps are done “atomically” (like with InterlockedIncrement) there can be no intervening references to the variable. The operations required, for the increment for example, aredone in the equivalent of a single operation.

So… to READ such a value, you don’t have to do anything special.

Does that help?

Peter

Or if variable is managed as atomic, all access has to be via atomic?

Of course not…

Just look at any spinlock implementation in existence (apart from the one provided by Mr.Kyler, of course) - you will see that an atomic operation takes place only in the inner loop, while the outer one reads the state of the flag with a MOV instruction. Furthermore, releasing a spinlock may be done as “regular” write as well.

The only situation when an atomic instruction makes sense is the one when read-update-write sequence has to be done atomically (line, for example, adding a value to a global variable that is not guarded by any synch construct). Otherwise, “regular” reads and writes are perfectly fine…

Anton Bassov

Peter,
Thanks for answer, it helps!

Anton,
thanks too, interesting example… :slight_smile: