Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ MT-multithreading and servers » Stop a thread
Stop a thread [message #20260] Thu, 05 March 2009 17:12 Go to next message
darthspawn is currently offline  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 #20262 is a reply to message #20260] Thu, 05 March 2009 19:10 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Unfortunately, the only safe way how to stop a thread known to us is to periodically test some shared variable for 'stop' status.

The reason is releasing of resources. To release resources, you need to call destructors, to do that correctly, you need synchronous termination.

Mirek
Re: Stop a thread [message #20266 is a reply to message #20260] Fri, 06 March 2009 08:34 Go to previous messageGo to next message
mr_ped is currently offline  mr_ped
Messages: 825
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).
Re: Stop a thread [message #20269 is a reply to message #20260] Fri, 06 March 2009 10:31 Go to previous message
darthspawn is currently offline  darthspawn
Messages: 58
Registered: February 2009
Member
Ok, thank you so much!
If you come to Italy i will offer to you a pizza Smile
Previous Topic: How to catch http client timeout error
Next Topic: how to exit from Socket.Read()
Goto Forum:
  


Current Time: Thu Mar 28 09:51:37 CET 2024

Total time taken to generate the page: 0.00941 seconds