Re: Boolean access in multithreaded environment
From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 05/21/04
- Next message: anamika: "Re: Debugging my app while it runs on the device"
- Previous message: Barry Bond: "Re: ExpandEnvironmentStrings"
- In reply to: ManojM: "Boolean access in multithreaded environment"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 21 May 2004 13:31:04 +0100
<<ManojM>> wrote:
> I have one thread assigning true/false to boolean and another thread
> accessing the value of the boolean. I need this operation to be thread safe.
> Someone told me that boolean assignment is atomic operation and thats why I
> need not make it volatile. Is it true that its atomic or should I make it
> volatile/restrict its access by lock?
Atomic access and volatilty are somewhat orthogonal issues. Atomic
access means that the value will always be either the old value or the
new value - never some half-way house where part of the new data has
been written and part of it hasn't.
Volatility is to do with whether or not a reading thread "sees" the new
value or whether it could use a cached copy, and whether a writing
thread actually accesses main memory or just changes a cached version
until the cache is flushed.
Restricting access using a lock sorts out both issues, as acquiring a
lock involves a volatile read, and releasing it involves a volatile
write.
-- Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
- Next message: anamika: "Re: Debugging my app while it runs on the device"
- Previous message: Barry Bond: "Re: ExpandEnvironmentStrings"
- In reply to: ManojM: "Boolean access in multithreaded environment"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|