is ternary operator atomic?

S

saurabh gupta

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
 
F

Francois Grieu

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
 
P

Peter Nilsson

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

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

There's no such guarantee since C has a fundamentally single
thread model.
No. There is simply no notion of atomicity in standard C.

Um... there is sig_atomic_t in said:
For C++, see next door, but no AFAIK.


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.
 
N

Nobody

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.
 
R

robertwessel2

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


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


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



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.
 
F

Francois Grieu

Le 01/07/2010 08:56, Peter Nilsson a écrit :
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top