Synchronization bool shared fields

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vladimir

    #1

    Synchronization bool shared fields

    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?


  • Jon Skeet [C# MVP]

    #2
    Re: Synchronization bool shared fields

    Vladimir <xozar@tut.by > wrote:[color=blue]
    > Is synchronization for bool shared fields required in multithreaded
    > application?[/color]

    Yes. It should either be volatile, or you should lock access.
    [color=blue]
    > 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?[/color]

    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@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Vladimir

      #3
      Re: Synchronization bool shared fields

      >> Is synchronization for bool shared fields required in multithreaded[color=blue][color=green]
      >> application?[/color]
      >
      > Yes. It should either be volatile, or you should lock access.
      >[color=green]
      >> 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?[/color]
      >
      > 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.[/color]

      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?


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Synchronization bool shared fields

        Vladimir <xozar@tut.by > wrote:[color=blue]
        > 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?[/color]

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

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        Working...