boost::thread join() and DLL unloading
My dll seems to be hanging in the when passed to regsvr32 I debugged through it and it is in deadlock on a boost::thread::join() The call stack tells me: The DLL is unloading The destructor to a global has been called That global contains a boost::thread object, on which join() has been called. (The boost::thread procedure has already exited) join() never returns. I've googled for a few hours and seen people mention something to the effect of "you cannot call join from DllMain" and "while DLL is unloading", but no explanation or alternative. How would I go about fixing this problem? I don't think I can guarentee noone will ever make a global instance of my class....
On 9/14/2011 4:48 PM, Christopher Pisz wrote:
I've googled for a few hours and seen people mention something to the effect of "you cannot call join from DllMain" and "while DLL is unloading", but no explanation or alternative.
How would I go about fixing this problem? I don't think I can guarentee noone will ever make a global instance of my class....
As I recall, the DLLMain's as well as process startup code in the main EXE is called with a process mutex, so threads cannot be started until the normal main is called. Likewise at shut-down, but I don't know what happens to other threads. I suspect they are halted before the unloading process begins. Once your main() returns, at some point threads are halted so ensure that threading is done before that! I assume you mean the DLL is unloading because the process is terminating. I don't know if that's still the case for dynamic LoadLibrary.
John M. Dlugosz
On 9/14/2011 4:48 PM, Christopher Pisz wrote:
I've googled for a few hours and seen people mention something to the
of "you cannot call join from DllMain" and "while DLL is unloading", but no explanation or alternative.
How would I go about fixing this problem? I don't think I can guarentee noone will ever make a global instance of my class....
As I recall, the DLLMain's as well as process startup code in the main EXE is called with a process mutex, so threads cannot be started until the normal main is called. Likewise at shut-down, but I don't know what happens to other threads. I suspect
before the unloading process begins. Once your main() returns, at some
effect they are halted point threads are
halted so ensure that threading is done before that!
I assume you mean the DLL is unloading because the process is terminating. I don't know if that's still the case for dynamic LoadLibrary.
Yes, at process terminatation, the dll gets unloaded, which calls a constructor, which attempts to join a thread in a class that has created a thread with its constructor. I get deadlock. I did some reading on MSDN and it seems there are severe restrictions on what can be done at dll load and unload time. So, I think the solution is to implement a c-style init() and shutdown() methods for the DLL to take care of any resources it has internally.
participants (2)
-
Christopher Pisz
-
John M. Dlugosz