cristalink <cr********@nospam.nospamwrote:
The below is perfectly OK in C++, but I wonder how safe is it C#.
Well, that depends on what you mean by safe. Your definition appears to
be a lot looser than mine. :)
There are cases when one thread is writing to, another one is reading
from the same variable.
The variable you've given is a long, which can't be made volatile. If
you're using a variable which could be declared volatile, you'd be
okay.
I don't want/need to synchronize the access.
Is this for performance reasons? If so, I *strongly* recommend that you
write code with full synchronization first and measure its performance.
Locking is very fast.
My question is, can the .NET framework handle this scenario, or it may throw
an exception? I realize the data may be out of sync (say, a half of the
variable contains new data, another half contains old data), but it's not an
issue.
You won't get an exception, but you might get half-changed data, or
worse you might not even see the new data at all from the reading
thread.
See
http://www.pobox.com/~skeet/csharp/t...latility.shtml for
more information.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too