473,379 Members | 1,278 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,379 software developers and data experts.

Is assignment to C++ int and pointer variables atomic?

Pardon if this is the wrong newsgroup for this question, and/or if this
question is naive.

I have a multi-threaded Windows application in which certain
variables/object fields are shared: one thread may write the variable,
and the other thread read it. The variables in question may have int or
int* types. Question: Is it safe to do this? Or is it possible a read
that happens at the same time as a write may retrieve a scrambled value,
in which, say, two of the bytes are from the old value and two of the
bytes from the new value?

In Java I know that such atomicity is guaranteed for ints, but not for
longs or doubles. So, perhaps I shouldn't assume it in a less
well-standardized environment

The application, btw, is a circular buffer with a read pointer and a
write pointer, which are moved forward by their respective threads. The
read thread needs to check to make sure that the read pointer is not on
the write pointer before reading. Using mutexes right now, but I worry
about their overhead...

Dave

Jan 3 '07 #1
9 15022
Dave Stallard wrote:
Pardon if this is the wrong newsgroup for this question, and/or if this
question is naive.
It's beyond the scope. The Standard doesn't specify threading behavior,
therefore any answer is implementation specific. You might want to ask
in a newsgroup dedicated to your platform.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.9

That said, I believe int is defined as the native integral type for an
implementation. So it *could* most likely be atomic. But then there's
alignment issues.

So the answer is "it depends, and check your compiler documentation".

Sorry we can't be more help.
Jan 3 '07 #2

red floyd wrote:
That said, I believe int is defined as the native integral type for an
implementation. So it *could* most likely be atomic. But then there's
alignment issues.

While int *could* be atomic on a given platform, it is certainly not
required. Consider, for example, an implementation of C on a typical
8-bit CPU - all ints will have to be accessed with a pair of loads or
stores.

Jan 3 '07 #3
Dave Stallard wrote:
Pardon if this is the wrong newsgroup for this question, and/or if this
question is naive.

I have a multi-threaded Windows application in which certain
variables/object fields are shared: one thread may write the variable,
and the other thread read it. The variables in question may have int or
int* types. Question: Is it safe to do this? Or is it possible a read
that happens at the same time as a write may retrieve a scrambled value,
in which, say, two of the bytes are from the old value and two of the
bytes from the new value?

In Java I know that such atomicity is guaranteed for ints, but not for
longs or doubles. So, perhaps I shouldn't assume it in a less
well-standardized environment

The application, btw, is a circular buffer with a read pointer and a
write pointer, which are moved forward by their respective threads. The
read thread needs to check to make sure that the read pointer is not on
the write pointer before reading. Using mutexes right now, but I worry
about their overhead...

Dave
As others have mentioned, C++ doesn't make any such guarantees.
However, since you mention you are running on Windows, you might look up
the InterlockedExchange, InterlockedExhangePointer,
InterlockedCompareExchange, etc. They can give you some guarantees at
the cost of portability between OSes.

See http://msdn2.microsoft.com/en-us/library/ms686360.aspx for details.

--
Alan Johnson
Jan 3 '07 #4

Note that I have added comp.programming.threads to this post.

Dave Stallard wrote:
Pardon if this is the wrong newsgroup for this question, and/or if this
question is naive.

I have a multi-threaded Windows application in which certain
variables/object fields are shared: one thread may write the variable,
and the other thread read it. The variables in question may have int or
int* types. Question: Is it safe to do this? Or is it possible a read
that happens at the same time as a write may retrieve a scrambled value,
in which, say, two of the bytes are from the old value and two of the
bytes from the new value?

comp.programming.threads is probably the right place to ask such questions.

Sharing variables like this depends on "visibility". Modern CPU's
heavily depend on cache and out of order execution for performance.
Normally, these effects are undetectable in a single threaded program
but they become critical when you're dealing with multi-threaded code.

So, once, a long long time ago if you wrote :

char ring_buffer[1024];
int wloc;
int rloc;

....writer...
ring_buffer[wloc] = ch; // S1
++wloc; // S2

....reader...
if ( wloc rloc )
{
val = ring_buffer[rloc]; //S3
++rloc; //S4
}

you could be guarenteed that effects of S1 would be visible before S2 to
the other thread. This is not the case now. S2 MAY be visible before
S1 which means that S3 may not see the value in "ch".

There are two reasons this may happen, one is the hardware (CPU) and the
other is the optimizing compiler using the volatile keyword. The second
is to make sure that the hardware compiles. This is totally hardware
dependant - no standard exists yet.

First thing to do is tell the compiler to not mess with the order:

volatile char ring_buffer[1024];
volatile int wloc;
volatile int rloc;

....writer...
ring_buffer[wloc] = ch; // S1
FENCE(); // make sure that S1 is visible
++wloc; // S2

....reader...
FENCE(); // make sure we see everything first
if ( wloc rloc )
{
val = ring_buffer[rloc]; //S3
++rloc; //S4
}

An excellent paper on the issues involved in standardizing a solution:
http://www.hpl.hp.com/techreports/20...-2004-209.html

Wikipedia gives an excellent high level perspective.
http://en.wikipedia.org/wiki/Memory_barrier

So, most code uses mutexes and these generally guarentee a memory
barrier, so if you use mutexes, you don't run into sequencing issues,
however they can be a significant performance hit. Having said that,
many and quite probably most applications will never need to worry about
the performance hit of mutexes.
>
In Java I know that such atomicity is guaranteed for ints, but not for
longs or doubles. So, perhaps I shouldn't assume it in a less
well-standardized environment

The application, btw, is a circular buffer with a read pointer and a
write pointer, which are moved forward by their respective threads. The
read thread needs to check to make sure that the read pointer is not on
the write pointer before reading. Using mutexes right now, but I worry
about their overhead...

Dave
Jan 3 '07 #5
red floyd wrote:
Dave Stallard wrote:
>Pardon if this is the wrong newsgroup for this question, and/or if this
question is naive.

It's beyond the scope. The Standard doesn't specify threading behavior,
therefore any answer is implementation specific. You might want to ask
in a newsgroup dedicated to your platform.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.9

That said, I believe int is defined as the native integral type for an
implementation. So it *could* most likely be atomic. But then there's
alignment issues.
Another thing that you probably need to worry about are caches on a
multiprocessor/multicore system. If you write an int in one thread, even
after the write operation is finished, the other thread might still see the
old value if it runs on the other CPU core because the change hasn't
propagated from one cache through RAM into the other cache.

Jan 3 '07 #6
On 2007-01-03 05:54, Dave Stallard wrote:
Pardon if this is the wrong newsgroup for this question, and/or if this
question is naive.

I have a multi-threaded Windows application in which certain
variables/object fields are shared: one thread may write the variable,
and the other thread read it. The variables in question may have int or
int* types. Question: Is it safe to do this? Or is it possible a read
that happens at the same time as a write may retrieve a scrambled value,
in which, say, two of the bytes are from the old value and two of the
bytes from the new value?
I can give you no guarantees but I'm sure it's quite safe to assume that
both reading and writing to an int (getting and setting its value) is
atomic. That, however, have very few applications, only when one thread
is writing only and the other thread is reading only. In most practical
applications you'll what to have a thread to first read the value, then
perhaps perform some check and then modify the value and write the
modified value back. These kinds of operations are not atomic on any
platform that I know about* not even in Java. The problem being that the
value can change between the read of the value and the write of the
modified value. To solve these issues you'll need to call syncronization
functions that are platform-specific.

* There are some architectures that supports a few selected atomic
operations, like compare and swap or compare and add.

--
Erik Wikström
Jan 3 '07 #7
"Gianni Mariani" <gi*******@mariani.wswrote in message
news:45**********************@per-qv1-newsreader-01.iinet.net.au...
>
Note that I have added comp.programming.threads to this post.

Dave Stallard wrote:
>Pardon if this is the wrong newsgroup for this question, and/or if this
question is naive.

I have a multi-threaded Windows application in which certain
variables/object fields are shared: one thread may write the variable,
and the other thread read it. The variables in question may have int or
int* types. Question: Is it safe to do this? Or is it possible a read
that happens at the same time as a write may retrieve a scrambled value,
in which, say, two of the bytes are from the old value and two of the
bytes from the new value?


comp.programming.threads is probably the right place to ask such
questions.

Sharing variables like this depends on "visibility". Modern CPU's heavily
depend on cache and out of order execution for performance. Normally,
these effects are undetectable in a single threaded program but they
become critical when you're dealing with multi-threaded code.
http://appcore.home.comcast.net/vzdo...c/static-init/
(section 2 deals with some of the issues involved)
Jan 4 '07 #8
http://appcore.home.comcast.net/vzdo...c/static-init/
(section 2 deals with some of the issues involved)
read this as well:

http://groups-beta.google.com/group/...3df394a0370fa6
Jan 4 '07 #9
Gianni Mariani wrote:
An excellent paper on the issues involved in standardizing a solution:
http://www.hpl.hp.com/techreports/20...-2004-209.html

Wikipedia gives an excellent high level perspective.
http://en.wikipedia.org/wiki/Memory_barrier

So, most code uses mutexes and these generally guarentee a memory
barrier, so if you use mutexes, you don't run into sequencing issues,
however they can be a significant performance hit. Having said that,
many and quite probably most applications will never need to worry about
the performance hit of mutexes.
Thanks, Gianni, for your very informative response, and thank you
everyone else who answered. I've taken my lesson, and will lock, lock,
lock in the future.

Dave
Jan 5 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: news | last post by:
I'm trying out the Zend Development Environment 4, and for the most part I like it, except for two things. It seems to think certain lines that are perfectly valid are bugs. Like this: while...
16
by: bluekite2000 | last post by:
I want Matrix A(B) to create shallow copy of B but A=B to create deep copy of B. Is that bad design? Why and why not?
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
4
by: Ina Schmitz | last post by:
Hi all, could anyone please tell me how to declare and use variables in DB2 UDB 8.2? I did it in SQL Server and Oracle without any problems: DECLARE @my_var FLOAT SET @my_var = -0.33; SELECT...
3
by: Zeng | last post by:
Hello, Is the assignment operation atomic? That is if one thread assigns to static variable an object and another uses the object assigned to the variable, would it be safe? Thanks! zeng
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
5
by: Richard | last post by:
Another of those "What's the DB2 equivalent to this Sybase syntax" I use local variables in Sybase SQL scripts to hold values for use later in the script. For example: declare @timenow...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.