Re: Thread and C++
On 30 Mar., 20:14, Ioannis Vranos <ivra...@freemail.spam.not.gr>
wrote:
Balog Pal wrote:
"Ioannis Vranos" <ivra...@freemail.spam.not.gr>
in C++, is the assignment to a primitive variable atomic? for exmpal=
e:
int i;
i++; //atomic?
i +2; //atomic?
No.
Must the above i be defined as volatile in multithreading environments=
?
Volatile helps little or nothing wrt threads in practice. It tells t=
he
compiler that the value could have changed, but you still have no clue =
of
the outcome of any read, write or i++ like complex, if at the same time
other thread also access the same object.
You need to use sync primitives (mutex, membar, system-provided atomic =
ops,
etc) to play safe, and also good understanding of the implications. =
And a
couple of reviews on any thread interaction-involved code and data...
I am talking about the case when mutexes (locks) are used. I am reading a=
bout Qt these days, and they
recommend using volatile definitions of built-in types in addition to mut=
exes, to avoid possible compiler
optimisations that may mess things, and I wonder if the last can be true,=
since we use mutexes.
First let me state that I believe that you are aware that volatile has
no purpose in a multithreaded environment. Using a mutex is necessary
and sufficient in C++ (for C++0x other solutions will be available).
Of course, there might be buggy compilers out there that do not behave
well with optimizations but behave properly when using volatile, but
this is the first time I've heard about such a case. Do they provide
more information?
/Peter