I have a little problem with a C++ class…
I have a private member bool clientConnected.
and a getter bool isClientConnected() {return clientConnected;}
now the proble is.. that clientConnected is set from a different thread.
in main I have a loop
while (!x.isClientConnected())
{}
now if I compile it in Debug config.
All works fine… while exits as soon as clientConnected is set to true.
but if I compile it in Release config.
The compiler optimizes the loop as its constant.
and makes the following:
00141C01 cmp al,bl
00141C03 je SDL_main+0A1h (141C01h)
the value of al is never updated again.
so its always thinks its false.
I tried also with volatile, same results
how can I prevent this optimization in class so the value will get updated on each call, without having to write something like
bool z = x.isClientConnected();
while (!z) { z = x.isClientConnected(); }
volatilehttp://msdn.microsoft.com/en-us/library/12a04hfd(v=vs.80).aspx looks like simple answer to your problem:But – you have loop taking CPU:
Better is to wait on semaphore:
How to create/use semaphore in MS Windows see: http://msdn.microsoft.com/en-us/library/windows/desktop/ms686946(v=vs.85).aspx