Are assignments guaranteed to be atomic ?

Discussion in 'C Programming' started by dm, Nov 9, 2005.

  1. dm

    dm Guest

    Hi,

    I'd like to know if the C standard can guarantee that an assignment to/from
    a variable will be atomic, that is will occur in a single indivisible
    instruction.

    For example, if I have the following code:

    int global_var;

    void set_global_var(int var) {
    global_var = var;
    }

    int get_global_var(void) {
    return global_var;
    }

    If two threads can call get_global_var and set_global_var, do I have to use
    a semaphore ?

    Thanks for your help.
    dm, Nov 9, 2005
    #1
    1. Advertising

  2. dm wrote:
    > Hi,
    >
    > I'd like to know if the C standard can guarantee that an assignment to/from
    > a variable will be atomic, that is will occur in a single indivisible
    > instruction.


    It does, but only because the C virtual machine is a single thread
    environment.

    However, if you want to talk about the language as applied to threaded
    contexts, then
    you'll need to consult another group, possibly one relating to your
    choice of threading
    mechanism.

    --
    Peter
    Peter Nilsson, Nov 9, 2005
    #2
    1. Advertising

  3. "dm" <> writes:
    > I'd like to know if the C standard can guarantee that an assignment to/from
    > a variable will be atomic, that is will occur in a single indivisible
    > instruction.
    >
    > For example, if I have the following code:
    >
    > int global_var;
    >
    > void set_global_var(int var) {
    > global_var = var;
    > }
    >
    > int get_global_var(void) {
    > return global_var;
    > }
    >
    > If two threads can call get_global_var and set_global_var, do I have to use
    > a semaphore ?


    Standard C has no support for threads, so the question isn't
    necessarily meaningful in that sense. But it does support signals.
    The <signal.h> header defines type sig_atomic_t; an object of that
    type can be accessed as an atomic entity, even in the presence of
    asynchronous interrupts. By implication, atomic access isn't
    guaranteed for any other type.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Nov 9, 2005
    #3
  4. dm

    Jordan Abel Guest

    On 2005-11-09, Peter Nilsson <> wrote:
    > dm wrote:
    >> Hi,
    >>
    >> I'd like to know if the C standard can guarantee that an assignment to/from
    >> a variable will be atomic, that is will occur in a single indivisible
    >> instruction.

    >
    > It does, but only because the C virtual machine is a single thread
    > environment.


    BZZT. This is not sufficient to make atomicity of assignments
    meaningless

    ....at file scope
    struct x {int a, int b} foo, bar;
    ....in some function
    foo = (struct x) {1, 2};
    bar = (struct x) {3, 4};
    ....more stuff happens
    bar = foo // a signal is raised during execution of this line

    Now, if the signal handler accesses bar, what does it see? is
    bar.b-bar.a guaranteed to be 1? Is it even guaranteed to be 1, 3 or -1?

    [imagine this happening to a long long or something that some cpu
    doesn't support natively, if you think the struct example is contrived]

    > However, if you want to talk about the language as applied to threaded
    > contexts, then you'll need to consult another group, possibly one
    > relating to your choice of threading mechanism.
    >
    Jordan Abel, Nov 9, 2005
    #4
  5. dm

    pete Guest

    Peter Nilsson wrote:
    >
    > dm wrote:
    > > Hi,
    > >
    > > I'd like to know if the
    > > C standard can guarantee that an assignment to/from
    > > a variable will be atomic,
    > > that is will occur in a single indivisible
    > > instruction.

    >
    > It does,


    I've never seen anything in the standard which indicates
    that assigning a value of 5 to an int,
    couldn't be rendered as a clear instruction,
    followed by 5 increments.

    I have seen x += 2 rendered as ++x,++x in disassembled code.

    --
    pete
    pete, Nov 9, 2005
    #5
  6. dm

    Alan Balmer Guest

    On Wed, 9 Nov 2005 05:04:49 +0100, "dm" <>
    wrote:

    >Hi,
    >
    >I'd like to know if the C standard can guarantee that an assignment to/from
    >a variable will be atomic, that is will occur in a single indivisible
    >instruction.
    >

    Not only does the standard not guarantee it, in most cases it won't be
    true.

    >For example, if I have the following code:
    >
    >int global_var;
    >
    >void set_global_var(int var) {
    > global_var = var;
    >}
    >
    >int get_global_var(void) {
    > return global_var;
    >}
    >
    >If two threads can call get_global_var and set_global_var, do I have to use
    >a semaphore ?
    >
    >Thanks for your help.
    >
    >

    --
    Al Balmer
    Balmer Consulting
    Alan Balmer, Nov 9, 2005
    #6
  7. dm

    Alan Balmer Guest

    On Wed, 09 Nov 2005 11:50:58 GMT, pete <> wrote:

    >Peter Nilsson wrote:
    >>
    >> dm wrote:
    >> > Hi,
    >> >
    >> > I'd like to know if the
    >> > C standard can guarantee that an assignment to/from
    >> > a variable will be atomic,
    >> > that is will occur in a single indivisible
    >> > instruction.

    >>
    >> It does,

    >
    >I've never seen anything in the standard which indicates
    >that assigning a value of 5 to an int,
    >couldn't be rendered as a clear instruction,
    >followed by 5 increments.
    >
    >I have seen x += 2 rendered as ++x,++x in disassembled code.


    Even in the given example, there will most likely be some variation of
    load from one location, then store to another.
    --
    Al Balmer
    Balmer Consulting
    Alan Balmer, Nov 9, 2005
    #7
  8. pete <> writes:
    > Peter Nilsson wrote:
    >> dm wrote:
    >> > Hi,
    >> >
    >> > I'd like to know if the
    >> > C standard can guarantee that an assignment to/from
    >> > a variable will be atomic,
    >> > that is will occur in a single indivisible
    >> > instruction.

    >>
    >> It does,

    >
    > I've never seen anything in the standard which indicates
    > that assigning a value of 5 to an int,
    > couldn't be rendered as a clear instruction,
    > followed by 5 increments.
    >
    > I have seen x += 2 rendered as ++x,++x in disassembled code.


    True, but you snipped some very relevant context:

    ] It does, but only because the C virtual machine is a single thread
    ] environment.

    If no concurrent access is possible, a clear followed by 5 increments
    might as well be atomic.

    It turns out that's not *quite* true because of signals, which is why
    the standard defines sig_atomic_t.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Nov 9, 2005
    #8
    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. mick

    $5 INSTANT PAYMENTS GUARANTEED

    mick, Jan 9, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    388
  2. Ben Fidge
    Replies:
    2
    Views:
    708
    Juan T. Llibre
    Sep 7, 2005
  3. Razvan
    Replies:
    5
    Views:
    4,431
    Razvan
    Sep 9, 2004
  4. Replies:
    10
    Views:
    7,724
    javabudy
    Jan 4, 2011
  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:
    211
    Robert Dober
    Jun 8, 2010
Loading...

Share This Page