A
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
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