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

Atomic operations and Thread safe code

consider this code :

int i; //gobal var

Thread1:
i=some value;

Thread2:
if (i==2) dosomething();
else dosomethingelse();

I want to write it to be thread safe without using synchronization
objects.

my questions are:

1) is an assignment operation of an int atomic in c/c++?
if so will thaat code work?

volatile int i;

int getI()
{ return i;//assignment is atomic so assignment to return
value will be atomic also
}

void setI(int somevalue)
{
i=somevalue;//assignment atomic
}

Thread1:
setI(somevalue);

Thread2:
if (getI()==2) dosomething();
else dosomethingelse();
2) what operations in c are atomic?
are mailslots pipes etc operations atomic/threadsafe ?
thanks.

Katy.

Mar 25 '06 #1
6 6221
bl************@hotmail.com wrote:
consider this code :

int i; //gobal var

Thread1:
i=some value;

Thread2:
if (i==2) dosomething();
else dosomethingelse();

I want to write it to be thread safe without using synchronization
objects.

my questions are:

1) is an assignment operation of an int atomic in c/c++?
if so will thaat code work?
No and no.
volatile int i;

int getI()
{ return i;//assignment is atomic so assignment to return
value will be atomic also
}

void setI(int somevalue)
{
i=somevalue;//assignment atomic
}

Thread1:
setI(somevalue);

Thread2:
if (getI()==2) dosomething();
else dosomethingelse();
2) what operations in c are atomic?
are mailslots pipes etc operations atomic/threadsafe ?


Modifications of objects of type sig_atomic_t (C99) are atomic in C. As
for C++ ask in a C++ newgroup.

However simply using atomic objects doesn't garuntee synchronisation
and threads as well as thread safe code are outside the scope of
standard C. Generally, you'll have to use external libraries like
pthreads or make use of specific API functions of your underlying
operating system.

For further questions on this topic please try to confine your postings
to more relavent groups like comp.programming.threads etc.

Mar 25 '06 #2
bl************@hotmail.com schrieb:
consider this code :

int i; //gobal var

Thread1:
i=some value;

Thread2:
if (i==2) dosomething();
else dosomethingelse();

I want to write it to be thread safe without using synchronization
objects.

my questions are:

1) is an assignment operation of an int atomic in c/c++?
if so will thaat code work?


I can't speak for C++, but probably the situation is similar;
ask in comp.lang.c++ to be sure.
In standard C, there are no threads; the C99 standard speaks
only of atomic operations with respect to contraction of
floating expressions.
Ask in a newsgroup for your operating system for the respective
brand of threads etc.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mar 25 '06 #3
Michael Mair wrote:
.... snip ...
the C99 standard speaks only of atomic operations with
respect to contraction of floating expressions.


I may have misunderstood, but what about operations on objects of type
<i>sig_atomic_t</i>?

Mar 25 '06 #4
In article <11*********************@z34g2000cwc.googlegroups. com>,
santosh <sa*********@gmail.com> wrote:
Modifications of objects of type sig_atomic_t (C99) are atomic in C.


I do not have my C89 standard handy at the moment. My recollection
is that in C89, the atomicity promises on sig_atomic_t only
hold in signal handlers and only for volatile sig_atomic_t.
The types of operations that are atomic
are restricted, but I do not recall the details at the moment.

The OP wanted to use atomic operations without using a synchronization
object. In C89, the minimum is to use a signal handler -- but if one
is using multiple threads then [generally speaking] one might be using
multiple processes, so it is possible that there are multiple signal
handlers active at any one time, so signal handlers should not be
considered to be a stand-in for thread synchronization.

In short, to do anything -useful- with sig_atomic_t requires relying
on implementation-specific extensions... and if you have those then
you should probably use a implementation-specific synchronization
primitive rather than risking very subtle race conditions that you
did not happen to think of.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Mar 25 '06 #5
santosh schrieb:
Michael Mair wrote:
... snip ...
the C99 standard speaks only of atomic operations with
respect to contraction of floating expressions.


I may have misunderstood, but what about operations on objects of type
<i>sig_atomic_t</i>?


Hmmm, good point. In principle, you have an integer type that
is an atomic entity w.r.t. _access_, even if you have interrupts.
The only kind of operations that do not have undefined behaviour
is assignment of a value to an object of type volatile sig_atomic_t.
This is effectively useless for meaningful portable programs --
as is the whole standard conforming part of C signal handling.

I consider <signal.h> as a suggestion how signal handling should be
done so that porting does not become too hard.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mar 25 '06 #6
In article <e0**********@canopus.cc.umanitoba.ca>,
Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:
The OP wanted to use atomic operations without using a synchronization
object. In C89, the minimum is to use a signal handler -- but if one
is using multiple threads then [generally speaking] one might be using
multiple processes, so it is possible that there are multiple signal
handlers active at any one time, so signal handlers should not be
considered to be a stand-in for thread synchronization.


Sorry, that should have read "multiple processors", not "multiple
processes". Some threading libraries can only have one of the threads
executing at any one instant, but other threading libraries can
distribute to multiple processors, with all the race conditions that
that implies.
Hmmm, I remember a second ago that the last time around we had
the atomicity debate, someone posted a reference to a software-only
synchronization method. It wasn't clear to me from the papers
whether that applied on multiple processors.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Mar 26 '06 #7

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

Similar topics

5
by: Paul Moore | last post by:
I can't find anything which spells this out in the manuals. I guess that, at some level, the answer is "a single bytecode operation", but I'm not sure that explains it for me. This thought was...
8
by: Glenn Kasten | last post by:
I am wondering which operations in Python are guaranteed to be atomic in the presence of multi-threading. In particular, are assignment and reading of a dictionary entry atomic? For example,...
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...
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...
0
by: blackstreetcat | last post by:
consider this code : int i; //gobal var Thread1: i=some value; Thread2: if (i==2) dosomething(); else dosomethingelse();
3
by: Ole Nielsby | last post by:
I need to implement reference counting in a class hierarch, in a thread safe manner. (The classes are umanaged but I might want to compile them with the /clr option.) Some of the objects - atoms...
9
by: Dave Stallard | last post by:
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...
11
by: japhy | last post by:
Is there a way to read a line (a series of characters ending in a newline) from a file (either by descriptor or stream) atomically, without buffering additional contents of the file?
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.