473,804 Members | 3,771 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help a practitioner with atomic access

Ark
Consider

static T foo;
..............
T get_foo(void) { return foo; }

In a multi-threaded (or -tasked) environment, I need to ensure that I
get_foo() grabs T atomically (e.g. is not preempted while accessing foo).

Are there types T for which atomic fetch /guaranteed/ portably? Does the
standard say anything on this? (I'd guess not; concurrency is not a
subject there.)

A related issue: if I modify get_foo() to
T get_foo(void)
{
T temp;
begin_critical( ); //whatever it means
temp = foo;
end_critical();
return temp;
}

- and foo is /not/ a volatile, is there a guarantee that temp will be
actually created, filled and returned as written? My fear is that if a
compiler is smart enough to figure out that begin_critical( ) and
end_critical() do not modify foo, it may optimize the code into

T get_foo(void)
{
begin_critical( );
end_critical();
return foo;
}

That I don't want to happen. On the other hand, I'd rather not make foo
volatile because while I am massaging it (within a critical section) I
don't want to turn off optimizations on it.

Any advice to a cornered practitioner?
Thanks,
- Ark


Oct 5 '06 #1
5 1876
Ark wrote:
Consider

static T foo;
.............
T get_foo(void) { return foo; }

In a multi-threaded (or -tasked) environment, I need to ensure that I
get_foo() grabs T atomically (e.g. is not preempted while accessing foo).

Are there types T for which atomic fetch /guaranteed/ portably? Does the
standard say anything on this? (I'd guess not; concurrency is not a
subject there.)
The closest you can come is `volatile sig_atomic_t', which
is guaranteed to work as expected w.r.t. signals. But signals
themselves are all set about with implementation-defined fever
trees, and there is no surety -- in the C language itself --
that signal-safety implies thread-safety.
A related issue: if I modify get_foo() to
T get_foo(void)
{
T temp;
begin_critical( ); //whatever it means
temp = foo;
end_critical();
return temp;
}

- and foo is /not/ a volatile, is there a guarantee that temp will be
actually created, filled and returned as written? My fear is that if a
compiler is smart enough to figure out that begin_critical( ) and
end_critical() do not modify foo, it may optimize the code into

T get_foo(void)
{
begin_critical( );
end_critical();
return foo;
}

That I don't want to happen. On the other hand, I'd rather not make foo
volatile because while I am massaging it (within a critical section) I
don't want to turn off optimizations on it.

Any advice to a cornered practitioner?
The magic is all in "whatever it means." All C gives you is
the notion of "sequence points," and then it whisks most of the
gift away under the provisions of the "as-if rule."

Other standards extend the C Standard and provide additional
guarantees not found in the C language per se. For example, the
POSIX "Pthreads" standard (I forget the exact number) guarantees
that pthread_mutex_l ock() and pthread_mutex_u nlock() will behave
as you desire w.r.t. the code executed in between them. It is
the job of the POSIX implementation to make sure this happens the
way you want; that may or may not require constraining some of the
optimizations a non-POSIX C compiler might indulge in.

See comp.programmin g.threads for much more on these topics;
they're really not about C as such. There you will learn (among
other things) that `volatile' is neither necessary nor sufficient
for what you want to do. Followups set.

--
Eric Sosman
es*****@acm-dot-org.invalid
Oct 5 '06 #2
Ark wrote:
Are there types T for which atomic fetch guaranteed portably? Does the
standard say anything on this? (I'd guess not; concurrency is not a
subject there.)

A related issue: if I modify get_foo() to
T get_foo(void)
{
T temp;
begin_critical( ); //whatever it means
temp = foo;
end_critical();
return temp;
}
Standard knows absolutely nothing about threads.

However, in your particular example, which I'm presuming to just be a
contrived example, I can't see how "T temp" is even suspect to being
clobbered by a concurrent action (presuming begin_critical( ) doesn't create
new threads and pass temp to it, etc.). I'm also figuring that you're
dyn-allocing for temp, otherwise you wouldn't be returning it either.
Oct 5 '06 #3
On Wed, 04 Oct 2006 22:07:06 -0400, Ark <ak*****@macroe xpressions.com>
wrote in comp.lang.c:
Consider

static T foo;
.............
T get_foo(void) { return foo; }

In a multi-threaded (or -tasked) environment, I need to ensure that I
get_foo() grabs T atomically (e.g. is not preempted while accessing foo).
If you have questions about multitasking, you really need to take them
to a group that supports your particular platform. The C standard
does not define any support at all for this.
Are there types T for which atomic fetch /guaranteed/ portably? Does the
standard say anything on this? (I'd guess not; concurrency is not a
subject there.)
The closest C comes to defining anything relating to multitasking is
the type sig_atomic_t, defined in <signal.h>, which is an integer type
that can be accessed atomically even in the presence of asynchronous
interrupts. It may need to be volatile for this guarantee, and the C
standard says nothing about whether your particular multitasking
environment is equivalent.
A related issue: if I modify get_foo() to
T get_foo(void)
{
T temp;
begin_critical( ); //whatever it means
I have no idea what that means either, ask in platform specific group.
temp = foo;
end_critical();
return temp;
}

- and foo is /not/ a volatile, is there a guarantee that temp will be
actually created, filled and returned as written? My fear is that if a
compiler is smart enough to figure out that begin_critical( ) and
end_critical() do not modify foo, it may optimize the code into

T get_foo(void)
{
begin_critical( );
end_critical();
return foo;
}

That I don't want to happen. On the other hand, I'd rather not make foo
volatile because while I am massaging it (within a critical section) I
don't want to turn off optimizations on it.
Just what terrible effect do you think making it volatile will have on
your program? As to what is or is not possible really depends on your
platform and its implementation of threads. Ask there.
Any advice to a cornered practitioner?
Thanks,
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Oct 5 '06 #4
Ark
Jack Klein wrote:
On Wed, 04 Oct 2006 22:07:06 -0400, Ark <ak*****@macroe xpressions.com>
wrote in comp.lang.c:
>Consider

static T foo;
............ .
T get_foo(void) { return foo; }

In a multi-threaded (or -tasked) environment, I need to ensure that I
get_foo() grabs T atomically (e.g. is not preempted while accessing foo).

If you have questions about multitasking, you really need to take them
to a group that supports your particular platform. The C standard
does not define any support at all for this.
>Are there types T for which atomic fetch /guaranteed/ portably? Does the
standard say anything on this? (I'd guess not; concurrency is not a
subject there.)

The closest C comes to defining anything relating to multitasking is
the type sig_atomic_t, defined in <signal.h>, which is an integer type
that can be accessed atomically even in the presence of asynchronous
interrupts. It may need to be volatile for this guarantee, and the C
standard says nothing about whether your particular multitasking
environment is equivalent.
>A related issue: if I modify get_foo() to
T get_foo(void)
{
T temp;
begin_critical( ); //whatever it means

I have no idea what that means either, ask in platform specific group.
> temp = foo;
end_critical();
return temp;
}

- and foo is /not/ a volatile, is there a guarantee that temp will be
actually created, filled and returned as written? My fear is that if a
compiler is smart enough to figure out that begin_critical( ) and
end_critical () do not modify foo, it may optimize the code into

T get_foo(void)
{
begin_critical( );
end_critical();
return foo;
}

That I don't want to happen. On the other hand, I'd rather not make foo
volatile because while I am massaging it (within a critical section) I
don't want to turn off optimizations on it.

Just what terrible effect do you think making it volatile will have on
your program? As to what is or is not possible really depends on your
platform and its implementation of threads. Ask there.
>Any advice to a cornered practitioner?
Thanks,
Gee... Yes it's easy to shove me to platforms or threads NG but... I
have no threads!
What I have is a small embedded system, a C compiler (IAR EWARM 4.40),
and a smallish RTOS (uC-OS/II) with a in-house-made port to the
platform. The compiler is unaware of this (or any) RTOS (yeah, the
debugger is). I know the habits of the compiler but I'd love my code to
be able to compile and run with a different compiler and/or on a
different target. You know, this portability thingy.
I wonder how much can be achieved in this direction. Turns out, not much...
Thank anyway.
- Ark
Oct 5 '06 #5
>On Wed, 04 Oct 2006 22:07:06 -0400, Ark <ak*****@macroe xpressions.com>
>>In a multi-threaded (or -tasked) environment, I need to ensure that I
get_foo() grabs T atomically ...
>Jack Klein wrote:
>If you have questions about multitasking, you really need to take them
to a group that supports your particular platform. The C standard
does not define any support at all for this.
Indeed.

In article <4c************ *************** ***@comcast.com >
Ark <ak*****@macroe xpressions.comw rote:
>Gee... Yes it's easy to shove me to platforms or threads NG but... I
have no threads!
In that case, you are stuck with platform- and/or compiler-specific.
>What I have is a small embedded system, a C compiler (IAR EWARM 4.40),
and a smallish RTOS (uC-OS/II) with a in-house-made port to the
platform. The compiler is unaware of this (or any) RTOS (yeah, the
debugger is). I know the habits of the compiler but I'd love my code to
be able to compile and run with a different compiler and/or on a
different target. You know, this portability thingy.
I wonder how much can be achieved in this direction. Turns out, not much...
Indeed, essentially none at all.

Or rather: you can (and apparently already have) define(d) your
own "start critical section" and "end critical section" macros
and/or functions, and write ones that work on your particular
compiler-plus-platform combination. Then, if you change one or
both of those, you need only rewrite those macros/functions.

(It turns out that some compilers, e.g., gcc, have general
"compiler-wide" mechanisms for preventing the compiler from moving
instructions across critical-section barriers. Combining these
with platform-specific barriers, such as the "memory barrier"
instruction on SPARCv9 or "eieio" instruction on PowerPC, does the
job. But note that *both* the gcc-specific trick *and* the
platform-specific trick are required here: if you use some other
compiler, even on the same CPU, you may need some other technique.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Oct 5 '06 #6

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

Similar topics

42
3368
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 of mutexes or semaphores. Thanks. Shayan
2
8909
by: ewu | last post by:
Hello, I am trying to implement a tree-structure data set on Microsoft Access using Joe Celko's nested sets (see http://www.intelligententerprise.com/001020/celko.shtml). According to Joe Celko, to insert a node into the tree, one has to UPDATE the ranges first and then INSERT the node. These queries should be atomic. Is there a way in Access to makes these queries atomic? If I use ASP 3.0 to manipulate the database, is there a way...
7
2173
by: dm | last post by:
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;
28
7420
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 "good" in such terms, that this core data structure is changed only by atomic operations, so that the data structure is always consistent regarding the application. Only the change-operations on the dicts and lists itself seem to cause problems...
0
9584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10583
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10337
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9160
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3822
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2995
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.