Re: Synchronization bool shared fields

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Vladimir <xozar@xxxxxx> wrote:
> 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.

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: Synchronization bool shared fields
    ... It should either be volatile, or you should lock access. ... > but you may not see the change when reading in the second thread unless ... when some volatile bool variable is true, i'm using them from first thread, ...
    (microsoft.public.dotnet.general)
  • Re: Thread.Resume is Obsolete?... My Final code - could you review?
    ... Jon Skeet [C# MVP] wrote: ... KEEP_RUNNING needs to be volatile, or you need to lock access to it. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Named shared memory without synchronization
    ... memory address with a single machine instruction. ... Reading an unaligned ... using the volatile keyword will ensure (at ... with VC8 using volatile would be ok. ...
    (microsoft.public.vc.language)
  • Re: is assignment atomic/thread safe?
    ... B could consistently ignore memory updates from A as long as it ignores ... I've yet to see any architecture where a programming environment that supports a "volatile" modifier cannot actually guarantee that code executed after a "volatile" variable has been assigned would not actually see the change to the "volatile" variable. ... But it _seems_ to me that you're reading too much into whatever you're seeing. ... That kind of delay should be very rare, but since threads may be suspended to allow the GC to take place, it's possible for the GC thread to monopolize the CPU for that length of time. ...
    (comp.lang.java.programmer)
  • Re: SATA_SIL works with 2.6.7-bk8 seagate drive, but oops
    ... Which describes how volatile is used to peek hardware status. ... So sata_sil.c is using the wrong construct or am I not reading it right? ... To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/ ...
    (Linux-Kernel)