Synchronization bool shared fields

  • Thread starter Thread starter Vladimir
  • Start date Start date
V

Vladimir

Is synchronization for bool shared fields required in multithreaded
application?

Is there a cases when for example changing value from true to false in one
thread, when another thread is reading this field, will lead to true value
in this field?
 
Vladimir said:
Is synchronization for bool shared fields required in multithreaded
application?

Yes. It should either be volatile, or you should lock access.
Is there a cases when for example changing value from true to false in one
thread, when another thread is reading this field, will lead to true value
in this field?

It won't stop the first thread from actually writing the change out,
but you may not see the change when reading in the second thread unless
you have a lock or make the flag volatile.
 
Is synchronization for bool shared fields required in multithreaded
Yes. It should either be volatile, or you should lock access.


It won't stop the first thread from actually writing the change out,
but you may not see the change when reading in the second thread unless
you have a lock or make the flag volatile.

Thanks...

But wait a minute...
I'm using some classes from two threads (time to time, but always not in one
moment -
when some volatile bool variable is true, i'm using them from first thread,
when false
- from second). What about shared data in this objects? Can the new state be
invisible
from another thread?
 
Vladimir said:
Thanks...

But wait a minute... I'm using some classes from two threads (time to
time, but always not in one moment - when some volatile bool variable
is true, i'm using them from first thread, when false - from second).
What about shared data in this objects? Can the new state be
invisible from another thread?

Yes, if you don't have any locking in place.
See http://www.pobox.com/~skeet/csharp/threads/volatility.shtml
 
Back
Top