c++ - Threads disappeared on DLL ExitInstance -
i have windows dll functions initinstance , exitinstance. dll creates threads, worker threads _beginthreadex , threads message queue, derived cwinthread (mfc).
the dll should useable application.
i wrote small host application dll test , worked fine, except when close host application without calling freelibrary before. in case, exitinstance called well, threads disappeard, quite unusual , leads deadlock routines waiting thread finished not exist longer - finished or killed. need go way (skip call freelibrary) in order simulate hapen when other applications use dll.
exitinstance called, threads still running disappeared - because dll handled somehow differently when unloaded host process if not call freelibrary before.
they disappear silently, instance, if thread implements loop waitforsingleobject within loop, thread not finish normally.
thread() { while(running == true) { waitforsingleobject(...); } threadfinished=true; /// 1 }
if calling freelibrary before closing application, code section 1 called. when closing application without calling freelibrary before, code section 1 never called loop not running longer thread removed.
how should handle situation ? thank you
the documentation cwinthread::exitinstance
clear: "do not call member function anywhere within run
member function." (i.e. cwinthread::run
, thread itself).
obviously, means windows not going call exitinstance
you. nor host application, since has no idea threads.
what is called dllmain
, once , argument dll_process_detach
. won't dll_thread_detach
because happens after threads exit cleanly (i.e. exitinstance
or returning cwinthread::run
).
by way, consider code threads running after dll has been unloaded. can't functions or mfc, since functions no longer exists. thread crash access violation because instruction pointer points unallocated memory.
Comments
Post a Comment