Re: C++ Threads, what's the status quo?
Hans wrote:
Assume I have a struct s containing fields x and y, both of which are
initially zero. Assume I have two threads executing
Thread 1: s.x = 1;
Thread 2: s.y = 1;
After I wait for both threads to complete (using your favorite threads
API), can I conclude that both s.x and s.y are one?
In real life, this corresponds to the question of whether x and y can
be protected by separate locks, which is important.
[snippage]
Yes, unless either
- x or y is a bit-field (in which case things get complicated)
- you are using default options to compile for an architecture like
Alpha which in prehistory did not have byte store instructions, and
either x or y is a byte in size, and they are "too close together"
- this is embedded in more complicated code, and your compiler decided
to combine multiple field updates in order to generate faster code
- your compiler was just being perverse, because the combined C, C++
and pthreads specs clearly don't require both fields to be one at the
end of this, even if they are both long integers.
I claim this is the wrong answer if you are trying to teach someone how
to write multithreaded code. This is the sort of problem we're trying
to fix.
[snippage]
I can see how the coin could flip either way. It depends on programmer
expectations. In all honesty, if I saw the example above, I would
probably give the reasons you listed, especially the one about the bit
fields.
So I think there is probably a differences of expectation. If the
compiler optimizes code to causes surprises, some people would rather
that not happen, "not" being used in the portable sense. Other people
might get bitten, and think, "Wow, I have to turn off the
optimizations."
My guess is that someone who has never written a multi-threaded
application, upon hearing that you and other experts are going to fix
the language so that they could have that, are going to be very
enthusiastic, and willing to wait. (Why suffer needlessly?)
But for me, I might be of the old guard, a Luddite who is willing to
spend effort to get the compilers to behave (I already have to do that
for other things).
If the standardization process is democratic, then of course, the
majority of programmers will get what they want/need.
-Le Chaud Lapin-
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]