is ternary operator atomic?

Discussion in 'C Programming' started by saurabh gupta, Jul 1, 2010.

  1. Hello All,

    I have a question regarding the ternary operator usage in C and C++.
    If I write,

    -----------------
    a = (a==9) ? 10 : 0; // a is an integer defined
    already
    ----------------------------------------


    Is the ternary operator statement atomic across all platforms. By
    atomic, i mean can it be guaranteed that while this statement is being
    executed, context will not be switched between multiple threads in the
    same process space. Is there any such mention in the C/C++ standard,
    Or it is compiler dependent.

    Thanks,
    Saurabh Gupta
     
    saurabh gupta, Jul 1, 2010
    #1
    1. Advertising

  2. On 01/07/2010 07:48, saurabh gupta wrote:

    > I have a question regarding the ternary operator usage in C and C++.
    > If I write,
    >
    > -----------------
    > a = (a==9) ? 10 : 0; // a is an integer defined
    > already
    > ----------------------------------------
    >
    >
    > Is the ternary operator statement atomic across all platforms. By
    > atomic, i mean can it be guaranteed that while this statement is being
    > executed, context will not be switched between multiple threads in the
    > same process space. Is there any such mention in the C/C++ standard,


    No. There is simply no notion of atomicity in standard C. For C++,
    see next door, but no AFAIK.

    > Or it is compiler dependent.


    Yes. Some platforms might give insurrance of atomicity, but typically
    this is using specialized intrinsic functions. Even a = a+1 would
    typically not be guaranteed atomic, much less a = (a==9) ? 10 : 0.

    Francois Grieu
     
    Francois Grieu, Jul 1, 2010
    #2
    1. Advertising

  3. Francois Grieu <> wrote:
    > saurabh gupta wrote:
    > > Is the ternary operator


    There is only one ternary operator in C, but it's actually
    called the conditional operator.

    > > statement


    It's an operator that can be used in expressions. It is not
    a statement in it's own right.

    > > atomic across all platforms. By atomic, i mean can it be
    > > guaranteed that while this statement is being executed,
    > > context will not be switched between multiple threads in
    > > the same process space.


    There's no such guarantee since C has a fundamentally single
    thread model.

    > > Is there any such mention in the C/C++ standard,

    >
    > No. There is simply no notion of atomicity in standard C.


    Um... there is sig_atomic_t in <signal.h>.

    > For C++, see next door, but no AFAIK.
    >
    > > Or it is compiler dependent.

    >
    > Yes. Some platforms might give insurrance of atomicity,
    > but typically this is using specialized intrinsic
    > functions. Even a = a+1


    Or even just a = 42; or even just a; if a is volatile.

    > would typically not be guaranteed atomic, much less
    >  a = (a==9) ? 10 : 0.


    --
    Peter
     
    Peter Nilsson, Jul 1, 2010
    #3
  4. saurabh gupta

    Nobody Guest

    On Wed, 30 Jun 2010 22:48:39 -0700, saurabh gupta wrote:

    > I have a question regarding the ternary operator usage in C and C++.
    > If I write,
    >
    > -----------------
    > a = (a==9) ? 10 : 0; // a is an integer defined
    > already
    > ----------------------------------------
    >
    >
    > Is the ternary operator statement atomic across all platforms. By
    > atomic, i mean can it be guaranteed that while this statement is being
    > executed, context will not be switched between multiple threads in the
    > same process space. Is there any such mention in the C/C++ standard,
    > Or it is compiler dependent.


    Nothing in C is atomic with regard to preemptive multithreading. C itself
    has no knowledge of threads, and threading implementations have no
    knowledge of C.
     
    Nobody, Jul 1, 2010
    #4
  5. saurabh gupta

    Guest

    On Jul 1, 1:56 am, Peter Nilsson <> wrote:
    > Francois Grieu <> wrote:
    > > saurabh gupta wrote:
    > > > Is the ternary operator

    >
    > There is only one ternary operator in C, but it's actually
    > called the conditional operator.
    >
    > > > statement

    >
    > It's an operator that can be used in expressions. It is not
    > a statement in it's own right.
    >
    > > > atomic across all platforms. By atomic, i mean can it be
    > > > guaranteed that while this statement is being executed,
    > > > context will not be switched between multiple threads in
    > > > the same process space.

    >
    > There's no such guarantee since C has a fundamentally single
    > thread model.
    >
    > > > Is there any such mention in the C/C++ standard,

    >
    > > No. There is simply no notion of atomicity in standard C.

    >
    > Um... there is sig_atomic_t in <signal.h>.



    Although the standard defines sig_atomic_t as only atomic as regards
    signals within the single thread model. OTOH, I know of no thread
    safe C implementations that don't make sig_atomic_t atomic with
    respect to threads as well.
     
    , Jul 1, 2010
    #5
  6. Le 01/07/2010 08:56, Peter Nilsson a écrit :
    > Francois Grieu <> wrote:
    >> There is simply no notion of atomicity in standard C.

    >
    > Um... there is sig_atomic_t in <signal.h>.


    Indeed. Thanks for pointing that out. It is of no help to
    what the OP wants to do, but interesting neverthless.

    "sig_atomic_t (..) is the (possibly volatile-qualified)
    integer type of an object that can be accessed as an
    atomic entity, even in the presence of asynchronous
    interrupts."

    If I get it correctly, one is at least certain that
    when such an object is assigned to a value in range
    [SIG_ATOMIC_MIN..SIG_ATOMIC_MAX], that assignment and
    corresponding read are atomic (after the variable
    is initialized, values read are among the values written
    or used at initialization).

    Is that right?

    Is the volatile keyword required for the declaration of
    such a sig_atomic_t?

    Francois Grieu
     
    Francois Grieu, Jul 1, 2010
    #6
    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. Jacob

    ternary operator error

    Jacob, Jun 30, 2003, in forum: Java
    Replies:
    12
    Views:
    4,736
    pete kirkham
    Jul 2, 2003
  2. Roger Leigh

    ternary operator and ostreams

    Roger Leigh, Jan 16, 2004, in forum: C++
    Replies:
    6
    Views:
    665
    Roger Leigh
    Jan 19, 2004
  3. marco_segurini
    Replies:
    4
    Views:
    806
    Dan Cernat
    Sep 21, 2004
  4. Paul E Johnson

    union, ternary operator, and C. What a mess!

    Paul E Johnson, Oct 17, 2003, in forum: C Programming
    Replies:
    3
    Views:
    464
    Ed Morton
    Oct 17, 2003
  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:
    234
    Robert Dober
    Jun 8, 2010
Loading...

Share This Page