c++ - How do I safely share a variable with a thread that is in a different compilation unit? -
in structure of program i've divided "where gets called from" , "what gets done" separate source files. matter of practicality, allows me compile program standalone or include in dll. code below not actual code simplified example makes same point.
there 3 interacting components here: kernel mode program loads dll, dll , source files , utility program it's source, maintained separately.
in dll form, program loaded thread. according kernel mode application vendor's documentation, loose ability call win32 api functions after initialization of kernel program load thread active thread (as opposed using create_suspended since can't wake it).
i have monitor flag variable knows when useful through inelegant functional:
while ( pausethreadflag ) sleep(1000);
the 1 second lag acceptable (the overall process lengthy, , infrequently called) , doesn't seem impact system.
in thread source file declare variable as
volatile bool pausethreadflag = true;
within dll source file i've declared
extern volatile bool pausethreadflag;
and when ready have thread execute, in dll set
pausethreadflag = false;
i've had difficulty in declaring std::string objects volatile, instead have declared parameters global variables within thread's source file , have dll call setters reside in thread's source. these strings have been parameters if instantiate thread @ will.
(missing of locking variable thread safety, next "to do")
this strikes me bad design ... it's functional convoluted. given constraints i've mentioned there better way go this?
i thinking possible revision use lpvoid lpparams variable given @ thread creation hold pointers string objects, though strings empty when thread created, , access them directly thread, way erasing declarations, setters, etc in thread program altogether? if works pause flag referenced there, , extern declarations eliminated (but think still needs declared volatile hint optimizer).
if makes difference, environment visual studio 2010, c++, target platform win32 (xp).
thanks!
if components running in kernel mode want take @ keinitializeevent, kesetevent, keresetevent , kewaitforsingleobject. these work in similar fashion user mode equivalents.
Comments
Post a Comment