Threads and simultaneous read and write

Discussion in 'C++' started by Piotr Jakubowski, Jan 14, 2011.

  1. If I have some primitive type or a pointer ands in one thread I change
    given variable/pointer and in the second one I read the same resource
    what would be the result of that situation? Will I randomly get one of
    two values (old or new) or would I get something totally screwed up?

    --
    Piotr Jakubowski
    Piotr Jakubowski, Jan 14, 2011
    #1
    1. Advertising

  2. Piotr Jakubowski

    Öö Tiib Guest

    On Jan 14, 10:30 pm, Piotr Jakubowski <> wrote:
    > If I have some primitive type or a pointer ands in one thread I change
    > given variable/pointer and in the second one I read the same resource
    > what would be the result of that situation? Will I randomly get one of
    > two values (old or new) or would I get something totally screwed up?


    Depends on platform. If writing or reading that type is not atomic on
    that platform then you may get screwed up thing.
    Öö Tiib, Jan 14, 2011
    #2
    1. Advertising

  3. On Jan 14, 12:30 pm, Piotr Jakubowski <> wrote:
    > If I have some primitive type or a pointer ands in one thread I change
    > given variable/pointer and in the second one I read the same resource
    > what would be the result of that situation? Will I randomly get one of
    > two values (old or new) or would I get something totally screwed up?


    By the formal POSIX, win32, and upcoming C++0x standards, you will
    have undefined behavior - that is your program may crash or do
    anything else it wants.

    In practice, it depends on the type of the pointer and the hardware.
    You might get the previous value, the next value, a mix if the
    hardware doesn't support atomically writing that size of data, and you
    might get some completely bogus value as well - what Java calls "out
    of thin air".
    Joshua Maurice, Jan 14, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dmoore
    Replies:
    8
    Views:
    525
    Nick Craig-Wood
    Jun 10, 2007
  2. Leo Jay
    Replies:
    0
    Views:
    324
    Leo Jay
    Aug 22, 2008
  3. Jean-Paul Calderone
    Replies:
    0
    Views:
    1,100
    Jean-Paul Calderone
    Aug 22, 2008
  4. Novice Coder
    Replies:
    27
    Views:
    616
    BCFD36
    Jan 14, 2012
  5. Steve D
    Replies:
    0
    Views:
    169
    Steve D
    Sep 4, 2003
Loading...

Share This Page