Are assignments guaranteed to be atomic ?

D

dm

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

Peter Nilsson

dm said:
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.
 
K

Keith Thompson

dm said:
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.
 
J

Jordan Abel

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]
 
P

pete

Peter said:

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

Alan Balmer

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

Alan Balmer

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

Keith Thompson

pete said:
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.
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top