Re: C++ Threads, what's the status quo?
James Kanze wrote:
Le Chaud Lapin wrote:
Mirek Fidler wrote:
Ahh... I guess you should seriously reconsider the issue.
Recommended reading is e.g. google: "why double checked guard does not
work".
Just for starters, if you have
Shared y;
Mutex y_lock;
void fn() {
y_lock.Lock();
y = 10;
y_lock.Unlock();
}
nothing in C++ standard prevents C++ compiler to generate machine code
equivalent to
void fn() {
y = 10;
y_lock.Lock();
y_lock.Unlock();
}
Are you saying that the C++ standard does not prescribe sequential
execution of statements in a single-threaded program?
It most certainly doesn't. Doing so simply wouldn't be
acceptable because of the performance cost. All C++ compilers,
and all C compilers I used before, have done code movement in
some cases. It's a standard optimization technique.
In this case, this is a problem that would be a serious issue, whether
we are talking about threading or not. If the compiler does not
guarantee order of execution, then it is very possible that Lock()
could depend on y being a certain value (not 10 for example).
So this is a serious issue in generally, and not necessarily related to
threading.
I am surprised that the compiler allows this.
-Le Chaud Lapin-
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]