Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: DLL with thread. Thread is not destroyed correctly, why?


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 4 - Last Post: Jan 19, 2017 11:06 AM Last Post By: Goran Ekstrom Threads: [ Previous | Next ]
Goran Ekstrom

Posts: 149
Registered: 1/10/04
DLL with thread. Thread is not destroyed correctly, why?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 18, 2017 1:30 PM
Hi,
I got a DLL Project in which I have a container class for miscellaneous data and functions placed in the DLL global data space. The class is autocreated at DLL load.

If I place this in the class constructor:

GD.WorkerThread.Handle = CreateThread(0, 0, &WorkerThreadFunction, NULL, 0, &GD.WorkerThread.ID);

... the thread is created and exeuted fine during the DLL's lifetime.

However, if I Place this in the class destructor:

GD.WorkerThread.Exit = true; // Causes the thread function to return
WaitForSingleObject(GD.WorkerThread.Handle, 5000);

... the WaitForSingleObject Waits forever. Why?

Right now I need to call DLL functions like StartDLLOp and StopDLLOp to handle the thread where the WaitForSingleObject method works fine.

Regards
Goran
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: DLL with thread. Thread is not destroyed correctly, why? [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 18, 2017 1:54 PM   in response to: Goran Ekstrom in response to: Goran Ekstrom
Goran wrote:

I got a DLL Project in which I have a container class for
miscellaneous data and functions placed in the DLL global data
space. The class is autocreated at DLL load.
If I place this in the class constructor:

GD.WorkerThread.Handle = CreateThread(0, 0, &WorkerThreadFunction,
NULL, 0, &GD.WorkerThread.ID);

... the thread is created and exeuted fine during the DLL's lifetime.

Consider yourself lucky. Your constructor is called within the context of
the DLL's entry point. There are only a few things you are allowed to do
safely in the entry point, and creating/destroying threads is not one of
them.

Dynamic-Link Library Best Practices
https://msdn.microsoft.com/en-us/library/windows/desktop/dn633971(v=vs.85).aspx

Does creating a thread from DllMain deadlock or doesn’t it?
https://blogs.msdn.microsoft.com/oldnewthing/20070904-00/?p=25283

Some reasons not to do anything scary in your DllMain
https://blogs.msdn.microsoft.com/oldnewthing/20040127-00/?p=40873

Another reason not to do anything scary in your DllMain: Inadvertent deadlock
https://blogs.msdn.microsoft.com/oldnewthing/20040128-00/?p=40853

Some reasons not to do anything scary in your DllMain, part 3
https://blogs.msdn.microsoft.com/oldnewthing/20140821-00/?p=183

The safer approach is to export functions from the DLL that the app will
have to call to (un)initialize the DLL as needed. Stay out of the entry
point as much as possible.

However, if I Place this in the class destructor:

GD.WorkerThread.Exit = true; // Causes the thread function to return
WaitForSingleObject(GD.WorkerThread.Handle, 5000);

... the WaitForSingleObject Waits forever. Why?

Because the destructor is also running in the context of the DLL's entry
point, and making the entry point wait for a thread to terminate is a guaranteed
deadlock scenario.

Right now I need to call DLL functions like StartDLLOp and StopDLLOp
to handle the thread where the WaitForSingleObject method works fine.

That is exactly what you should be doing.

--
Remy Lebeau (TeamB)
Goran Ekstrom

Posts: 149
Registered: 1/10/04
Re: DLL with thread. Thread is not destroyed correctly, why? [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 18, 2017 3:12 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Right now I need to call DLL functions like StartDLLOp and StopDLLOp
to handle the thread where the WaitForSingleObject method works fine.

That is exactly what you should be doing.

Good, so I'll keep that then. I should also remove both the constructor and destructor then to prevent possible future problems?

Regards
Goran
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: DLL with thread. Thread is not destroyed correctly, why? [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 18, 2017 3:44 PM   in response to: Goran Ekstrom in response to: Goran Ekstrom
Goran wrote:

I should also remove both the constructor and destructor
then to prevent possible future problems?

That depends on what they actually do. Like I said earlier, some things
are safe, some things are not. On the other hand, you could just have StartDLLOp()
construct the object and StopDLLOp() destroy it.

--
Remy Lebeau (TeamB)
Goran Ekstrom

Posts: 149
Registered: 1/10/04
Re: DLL with thread. Thread is not destroyed correctly, why? [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 19, 2017 11:06 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
I should also remove both the constructor and destructor
then to prevent possible future problems?

That depends on what they actually do...

Yes, that is right. For now I'll leave it as it is.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02