By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,498 Members | 1,740 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,498 IT Pros & Developers. It's quick & easy.

Are assignments guaranteed to be atomic ?

P: n/a
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.

Nov 15 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

Nov 15 '05 #2

P: n/a
"dm" <ma*********@wanadoo.fr> 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) ks***@mib.org <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.
Nov 15 '05 #3

P: n/a
On 2005-11-09, Peter Nilsson <ai***@acay.com.au> 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.

Nov 15 '05 #4

P: n/a
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
Nov 15 '05 #5

P: n/a
On Wed, 9 Nov 2005 05:04:49 +0100, "dm" <ma*********@wanadoo.fr>
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
re************************@att.net
Nov 15 '05 #6

P: n/a
On Wed, 09 Nov 2005 11:50:58 GMT, pete <pf*****@mindspring.com> 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
re************************@att.net
Nov 15 '05 #7

P: n/a
pete <pf*****@mindspring.com> 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) ks***@mib.org <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.
Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.