c++ - Efficient exit from multithreaded application (specifics) -


i've read few sources on proper methods of bubbling message out thread other threads exit gracefully (every thread performs it's own exit routine). of these, liked idea of global atomic boolean can flagged thread, , other threads check flag perform exit routine - when threads joined, main thread can exit application.

purely computation threads handled differently, right?

is efficient , safe? there better way this?

thanks!

in windows, use queueuserapc call function throws exception, causing threads exit cleanly.

i wrote more details in answer here:

how guarantee fast shutdown of win32 app?

in summary, here's happens:

say thread wants terminate thread b (and c, d, ...)

  • thread calls queueuserapc(), passing handle thread b , address of function throw exception of class mythreadexit.
  • thread b runs until calls checks alertable waits. maybe waitforsingleobjectex, maybe sleepex, or else.
  • at point, thread b runs apc function passed earlier, causing exception thrown in thread b.
  • all stack-allocated objects automatically destructed correctly exception makes thread b 'unwind' stack.
  • the outermost thread function of thread b catch exception.
  • thread b exits, possibly signalling thread it's done.

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -