Home » U++ Library support » U++ MT-multithreading and servers » Stop a thread
Stop a thread [message #20260] |
Thu, 05 March 2009 17:12  |
darthspawn
Messages: 58 Registered: February 2009
|
Member |
|
|
I don't understand how to stop a thread, I see the GuiMT example, but I don't know what is "volatile atomic", and how the thread can terminate.
Many thanks to everyone will me explain this!
|
|
|
|
Re: Stop a thread [message #20266 is a reply to message #20260] |
Fri, 06 March 2009 08:34   |
mr_ped
Messages: 826 Registered: November 2005 Location: Czech Republic - Praha
|
Experienced Contributor |
|
|
darthspawn wrote on Thu, 05 March 2009 17:12 | "volatile atomic"
|
volatile = C/C++ keyword, it tells compiler the value can change from "outside" in memory while the current code is running. With general variables like in:
bool run = true;
while ( run ) {
run = false; //change "inside" code
}
The compiler can optimize the whole code to read the content of variable from memory into register once, and then work only with register and ignore the memory.
So if the value would change only in the memory outside of the current code, the final code will not catch it and would run in infinite loop.
With "volatile" the compiler knows the value has to be re-read from memory all the time to keep it fresh and allow modifications from "outside" to be catch ASAP.
Atomic is defined as "long" in Mt.h, and that's because reading/writing of long at x86 is atomic operation, i.e. you either did read/write whole long, or you didn't at all, you can't get interrupted in between. Actually there's special machine code instruction to compare+exchange 8/16/32b value atomically (CMPXCHG and CMPXCHG8B), which allows you to write some new value into memory only if the old value didn't change yet (very handy in MT where more then one thread access and change the same memory variable and need to cooperate in some way, like doing auto-increment of IDs for example).
|
|
|
|
Goto Forum:
Current Time: Fri May 09 11:24:41 CEST 2025
Total time taken to generate the page: 0.01002 seconds
|