On Mar 7, 8:26 am, "Joseph Geretz" <jger...@nospam.comwrote:
Hi Oscar,
you can use a flag
bool run = true
while(run){
// Do some work
// Check condition
run = false;
}
So what you're saying is that when the method which is running on the thread
terminates, the thread itself is terminated automatically?
Thanks for your help,
- Joseph Geretz -
Yes, when a thread method exits - so does the thread. Be a little
careful using a flag like this though to end the thread - especially
if it is going to be set from outside the thread... While on x86
processors, reads and writes of this type are guarenteed to be atomic
- you may still get unexpected results. The reason is that sometime
the compiler will optimize the code to read from a register instead of
the actual memory value. When this happens in a threaded environment,
you may set the value to false - but the thread will never see it
happen... You have two choices to fix this - one use a lock or
declare the bool as volatile (which would be my prefered method in
this case :). By declaring it volatile, you tell the compiler that
this value maybe changed from an outside source, and to always fetch
it from memory.
--
Tom Shelton