Is assignment to pointer atomic?

Discussion in 'C++' started by Christopher, Aug 12, 2011.

  1. Christopher

    Christopher Guest

    I disagree with my cooworker, but neither of us can seem to find any
    documentation to support our belief.
    Is assignment to a pointer guarenteed to be an atomic operation?
    Christopher, Aug 12, 2011
    #1
    1. Advertising

  2. On 8/12/2011 10:47 AM, Christopher wrote:
    > I disagree with my cooworker, but neither of us can seem to find any
    > documentation to support our belief.
    > Is assignment to a pointer guarenteed to be an atomic operation?


    Not if said assignment takes more than one processor instruction,
    probably. The new standard has a special section on atomics, perhaps
    that's what you need to use... See the header <atomic>.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 12, 2011
    #2
    1. Advertising

  3. Christopher

    Guest

    On Aug 12, 10:10 am, Victor Bazarov <> wrote:
    > On 8/12/2011 10:47 AM, Christopher wrote:
    >
    > > I disagree with my cooworker, but neither of us can seem to find any
    > > documentation to support our belief.
    > > Is assignment to a pointer guarenteed to be an atomic operation?

    >
    > Not if said assignment takes more than one processor instruction,
    > probably.  The new standard has a special section on atomics, perhaps
    > that's what you need to use...  See the header <atomic>.
    >


    Even if it's a single processor instruction it's not guarenteed to be
    atomic.
    Read your processors manual to know when and when it isn't.

    Martin Shobe
    , Aug 12, 2011
    #3
  4. Christopher

    Christopher Guest

    On Aug 12, 11:04 am, "" <>
    wrote:
    > On Aug 12, 10:10 am, Victor Bazarov <> wrote:
    >
    > > On 8/12/2011 10:47 AM, Christopher wrote:

    >
    > > > I disagree with my cooworker, but neither of us can seem to find any
    > > > documentation to support our belief.
    > > > Is assignment to a pointer guarenteed to be an atomic operation?

    >
    > > Not if said assignment takes more than one processor instruction,
    > > probably.  The new standard has a special section on atomics, perhaps
    > > that's what you need to use...  See the header <atomic>.

    >
    > Even if it's a single processor instruction it's not guarenteed to be
    > atomic.
    > Read your processors manual to know when and when it isn't.
    >
    > Martin Shobe


    Well, one would then conclude it depends not only on the compiler, but
    also the processor. If we don't want to enforce the limitation that
    the code should be compiled and executed on a particular processor,
    and we wish to be as close to standard C++ as possible, then we should
    indeed use a mutex around pointer assignment, no?
    Christopher, Aug 12, 2011
    #4
  5. On 8/12/2011 12:16 PM, Christopher wrote:
    > On Aug 12, 11:04 am, ""<>
    > wrote:
    >> On Aug 12, 10:10 am, Victor Bazarov<> wrote:
    >>
    >>> On 8/12/2011 10:47 AM, Christopher wrote:

    >>
    >>>> I disagree with my cooworker, but neither of us can seem to find any
    >>>> documentation to support our belief.
    >>>> Is assignment to a pointer guarenteed to be an atomic operation?

    >>
    >>> Not if said assignment takes more than one processor instruction,
    >>> probably. The new standard has a special section on atomics, perhaps
    >>> that's what you need to use... See the header<atomic>.

    >>
    >> Even if it's a single processor instruction it's not guarenteed to be
    >> atomic.
    >> Read your processors manual to know when and when it isn't.
    >>
    >> Martin Shobe

    >
    > Well, one would then conclude it depends not only on the compiler, but
    > also the processor. If we don't want to enforce the limitation that
    > the code should be compiled and executed on a particular processor,
    > and we wish to be as close to standard C++ as possible, then we should
    > indeed use a mutex around pointer assignment, no?


    If you can use a compiler that implements the new 'atomic' type, you
    should. That will take care of the synchronization and exclusivity *if*
    it's needed. See Chapter 29 of the new Standard.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 12, 2011
    #5
  6. Christopher <> wrote:
    > I disagree with my cooworker, but neither of us can seem to find any
    > documentation to support our belief.
    > Is assignment to a pointer guarenteed to be an atomic operation?


    AFAIK the current C++ standard takes no stance whatsoever about the
    atomicity of any operation (which means it's implementation-dependent).

    The next standard will have some support for explicit atomic operations,
    but I'm assuming that without the explicit keyword the atomicity of any
    operation will still be undefined.

    Whether it's an atomic operation eg. on an x86 or an x86-64 processor
    artchitecture, I don't know. (Although it would be an interesting tidbit.)
    Juha Nieminen, Aug 12, 2011
    #6
  7. On Aug 12, 7:47 am, Christopher <> wrote:
    > I disagree with my cooworker, but neither of us can seem to find any
    > documentation to support our belief.
    > Is assignment to a pointer guarenteed to be an atomic operation?


    What do you mean by "atomic"? There are several possible uses here.
    Are we talking "atomic" in terms of signal handlers? "Atomic" in terms
    of threading? "Atomic" in terms of MMIO?
    Joshua Maurice, Aug 12, 2011
    #7
    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. Dave Stallard
    Replies:
    9
    Views:
    586
    Dave Stallard
    Jan 5, 2007
  2. Philipp
    Replies:
    7
    Views:
    464
    Volker Borchert
    May 6, 2008
  3. Mark Thornton

    Re: is assignment atomic/thread safe?

    Mark Thornton, Feb 20, 2009, in forum: Java
    Replies:
    24
    Views:
    2,182
    Arne Vajhøj
    Feb 21, 2009
  4. Tom Anderson

    Re: is assignment atomic/thread safe?

    Tom Anderson, Feb 20, 2009, in forum: Java
    Replies:
    5
    Views:
    683
    Mark Space
    Feb 22, 2009
  5. Charles Oliver Nutter

    [ANN] atomic 0.0.1 - An atomic reference for Ruby

    Charles Oliver Nutter, Jun 8, 2010, in forum: Ruby
    Replies:
    5
    Views:
    229
    Robert Dober
    Jun 8, 2010
Loading...

Share This Page