S
S
sitev_ru2016-07-19 11:48:14
C++ / C#
sitev_ru, 2016-07-19 11:48:14

Why does WaitForSingleObject work incorrectly in DLL_PROCESS_DETACH?

I am writing a DLL.
I call the 1st function, inside it I create a thread:

hThread = CreateThread(NULL, 0, MyThreadFunction, ...);

I want to end the thread, I call the 2nd function:
StopMyThread(hThread); //моя фукнция
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);

Everything works correctly.
Now I want to close the thread if the program that is using the dll calls FreeLibrary. To do this, I process DLL_PROCESS_DETACH in dllmain. I am writing the same code as for the 2nd function, but hangs on WaitForSingleObject(hThread, INFINITE).
Why is this happening?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
sitev_ru, 2016-07-19
@sitev_ru

When DLLMain is called, the system critical section is called, which is also used in functions such as: WaitForSingleObject and others. Replaced WaitForSingleObject with a self-written one - it all worked!

J
jcmvbkbc, 2016-07-19
@jcmvbkbc

From the DllMain help :

Because DLL notifications are serialized, entry-point functions should not attempt to communicate with other threads or processes. Deadlocks may occur as a result.

P
Peter, 2016-07-19
@petermzg

You have the same as here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question