is ternary operator atomic?

  • Follow


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
0
Reply saurabhgupta1403 (1) 7/1/2010 5:48:39 AM

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
0
Reply Francois 7/1/2010 6:00:46 AM


Francois Grieu <fgr...@gmail.com> 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 =3D a+1

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

> would typically not be guaranteed atomic, much less
> =A0a =3D (a=3D=3D9) ? 10 : 0.

--
Peter
0
Reply Peter 7/1/2010 6:56:18 AM

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.
0
Reply Nobody 7/1/2010 7:03:09 AM

On Jul 1, 1:56=A0am, Peter Nilsson <ai...@acay.com.au> wrote:
> Francois Grieu <fgr...@gmail.com> 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.
0
Reply robertwessel2 7/1/2010 7:21:21 AM

Le 01/07/2010 08:56, Peter Nilsson a �crit :
> Francois Grieu <fgrieu@gmail.com> 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
0
Reply Francois 7/1/2010 7:33:57 AM

5 Replies
411 Views

(page loaded in 0.091 seconds)

Similiar Articles:





7/22/2012 6:49:20 PM


Reply: