Re: Am I or Alexandrescu wrong about singletons?
On Wed, 31 Mar 2010 02:56:56 CST, Joshua Maurice
<joshuamaurice@gmail.com> wrote:
On Mar 30, 8:14 pm, Herb Sutter <herb.sut...@gmail.com> wrote:
On Tue, 30 Mar 2010 16:37:59 CST, James Kanze <james.ka...@gmail.com>
wrote:
(I keep seeing mention here of instruction reordering. In the
end, instruction reordering is irrelevant. It's only one thing
that may lead to reads and writes being reordered.
Yes, but: Any reordering at any level can be treated as an instruction
reordering -- actually, as a source code reordering. That's why all
language-level MM discussions only bother to talk about source-level
reorderings, because any CPU or cache transformations end up having
the same effect as some corresponding source-level reordering.
Not quite, no.
You mistake what we mean by "reordering" -- see note at bottom.
On "weaker guarantee" processors, let's take the
following example:
/*
start pseudo code example. Forgive me for any "typos". This is off the
top of my head and I haven't really used lambda functions.
*/
int main()
{ int a = 0;
int b = 0;
int c[4];
int d[4];
start_thread([&]() -> void { c[0] = a; d[0] = b; });
start_thread([&]() -> void { c[1] = a; d[1] = b; });
start_thread([&]() -> void { c[2] = a; d[2] = b; });
start_thread([&]() -> void { c[3] = a; d[3] = b; });
a = 1;
b = 2;
cout << c[0] << " " << d[0] << '\n'
<< c[1] << " " << d[1] << '\n'
<< c[2] << " " << d[2] << '\n'
<< c[3] << " " << d[3] << endl;
}
//end pseudo code example
On some modern processors, most (in)famously the DEC Alpha with its
awesome split cache, this program in the real world (or something very
much like it) can print:
0 0
0 2
1 0
1 2
Specifically, this is a single execution of the program. In this
single execution, the writes "a = 1; b = 2;" are seen to happen in two
different orders, the exact same "store instructions" become visible
to other cores in different orders. There is no (sane) source code
level reordering that can achieve this.
Sure there is, in fact a single reordering of two statements will do:
Reorder *only* the second thread (to make it be "d[1] = b; c[1] =
a;"), then the above occurs in several possible interleavings.
I tried to emphasize this else-
thread: you cannot think about threading in terms of "possible
interleavings of instructions".
^^^^^^^^^^^^^
Aha, I see our disconnect. My point wasn't about "interleavings" but
rather "reorderings." Here, "reorderings" doesn't mean possible
interleavings of the source code as written, it's about actually
permuting the source code to execute operations (notably, memory
operations) out of order (as compilers, processors, and caches are all
wont to do).
Herb
---
Herb Sutter (herbsutter.wordpress.com) (www.gotw.ca)
Convener, SC22/WG21 (C++) (www.gotw.ca/iso)
Architect, Visual C++ (www.gotw.ca/microsoft)
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]