c++ - How to control thread lifetime using C++11 atomics -


following on this question, i'd know what's recommended approach should take replace common pattern have in legacy code.

we have plenty of places primary thread spawing 1 or more background worker threads , periodically pumping out work them do, using suitably synchronized queue. general pattern worker thread this:

there event handle , bool defined somewhere (usually member variables) -

handle hdosomething = createevent(null, false, false, null); volatile bool bendthread = false; 

then worker thread function waits event signalled before doing work, checks termination request inside main loop -

unsigned int threadfunc(void *pparam) {     // typical legacy implementation of worker thread     while (true)     {         // wait event         waitforsingleobject(hdosomething, infinite);          // check termination request         if (bendthread) break;          // ... background work ...     }      // normal termination     return 0; } 

the primary thread can give work background thread -

// ... put work on synchronized queue ...  // pulse worker thread work setevent(hdosomething); 

and can terminate worker thread -

// terminate worker thread bendthread = true; setevent(hdosomething);  // wait worker thread die waitforsingleobject(hworkerthreadhandle, dwsomesuitabletimeout); 

in cases, we've used 2 events (one work, 1 termination) , waitformultipleobjects instead, general pattern same.

so, looking @ replacing volatile bool c++11 standard equivalent, simple replacing this

volatile bool bendthread = false; 

with this?

std::atomic<bool> bendthread = false; 

i'm sure work, doesn't seem enough. also, doesn't affect case use 2 events , no bool.

note, i'm not intending replace legacy stuff ppl and/or concurrency runtime equivalents because although use these new development, legacy codebase end-of-life , needs compatible latest development tools (the original question linked above shows concern arose).

can give me rough example of c++11 standard code use simple thread management pattern rewrite our legacy code without refactoring?

if ain't broken don't fix (especially if legacy code base)

  1. vs style volatile around few more years. given mfc isn't dead won't dead time soon. cursory google search says can control /volatile:ms.

  2. atomics might job of volatile, if counter there might little performance overhead.

  3. many windows native functions have different performance characteristics when compared c++11 implementation. example, windows timerqueues , multimedia have precision not possible achieve c++11.

    for example ::sleep_for(5) sleep 15 (and not 5 or 6). can solved mysterious call timesetperiod. example unlocking on condition variable can slow respond. interfaces fix these aren't exposed c++11 on windows.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -