Atomic operations and Thread safe code

B

blackstreetcat

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

santosh

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

Michael Mair

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
 
S

santosh

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>?
 
W

Walter Roberson

santosh said:
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.
 
M

Michael Mair

santosh said:
Michael Mair wrote:
... snip ...


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
 
W

Walter Roberson

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.
 

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,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top