I discovered an odd implementation of Stack class in FCL. As I see, Stack class provides SyncRoot property intended to allow external users to block the entire collection and work with it in a thread safe manner. But along with SyncRoot I discovered a private field _version incrementing by Stack class in modifying operations (Push, Pop, Clear). This field is utilized by StackEnumerator class to check if there were any changes while looping through a collection. But what a surprize! Everywhere Stack class increments version by unary operator ++ without Interlock class using. So I quiete don't understand what is the intention of a version field?
Asked
Active
Viewed 70 times
1
-
I think you are correct. `Interlocked.Increment` should be used. See also http://stackoverflow.com/questions/11745440/what-operations-are-atomic-in-c – Richard Schneider Jun 18 '13 at 05:42