473,324 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Are assignments guaranteed to be atomic ?

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
7 2133
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
"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Shayan | last post by:
Is there a boolean flag that can be set atomically without needing to wrap it in a mutex? This flag will be checked constantly by multiple threads so I don't really want to deal with the overhead...
3
by: bearophileHUGS | last post by:
The current version of ShedSkin (http://shedskin.sourceforge.net/ experimental Python to C++ compiler) is rather alpha, and the development work is focused on debugging and implementing some more...
5
by: Pegboy | last post by:
What does it mean to make a function atomic? Something I read on it wasn't very clear, but made me think that I needed to disable interrupts for that function. Whether that's the case or not,...
28
by: robert | last post by:
In very rare cases a program crashes (hard to reproduce) : * several threads work on an object tree with dict's etc. in it. Items are added, deleted, iteration over .keys() ... ). The threads are...
6
by: blackstreetcat | last post by:
consider this code : int i; //gobal var Thread1: i=some value; Thread2: if (i==2) dosomething(); else dosomethingelse();
1
by: Roberto Orsini | last post by:
Hi, my question is: are variable assignments always atomic? I reckon they are for most primitive data types and class references. What about decimals? And structs? Thanks.
17
by: Brian Blais | last post by:
Hello, I have a couple of classes where I teach introductory programming using Python. What I would love to have is for the students to go through a lot of very small programs, to learn the...
2
by: Freedom fighter | last post by:
Hello, Is a singleton class the same as an atomic class? I know that a singleton class can only be instantiated once, but does that concept apply to an atomic class? Thank you.
11
by: Jon Harrop | last post by:
Can read locks on a data structure be removed safely when updates are limited to replacing a reference? In other words, is setting a reference an atomic operation? I have been assuming that all...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.