Click here to Skip to main content
15,911,132 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
Hello i am facing this problem were i signal a thread to exit by itself, it gets signal and exits but after i signal the thread to exit i have a waitForSingleObject call to wait until the thread exits, like this:


<pre lang="c++">
//signal thread to terminate
 SetEvent ( hEvent );

//wait until thread exits
 WaitForSingleObject( hThread, INFINITE );

</pre>

this code works in an exe project, but not in a dll!,when the thread exits hThread never gets signal and  WaitForSingleObject stays for ever but in an exe project it works, using _beginthreadex, AfxBeginThread or CreateThread all in the exe project work but not in the dll one.

What is wrong?, do i have to do something extra? :|

Thanks
Posted

What's wrong? Everything.

Not clear what you want to achieve, but isn't that obvious: SetEvent(hEvent) is called to wake up some other thread which called WaitForSingleObject(hEvent, INFINITE) with the same event handle?! What happens later, depends on how you initialized the event: if it is auto-reset, it will get reset when the waiting thread is awaken, otherwise it will stay set.

By the way, auto-reset provides non-trivial effect which cannot be simulated by other things and logical operator: the reset is atomic, it is designed to pass one and only one thread at a time and keep any number of other waiting threads in a wait state until another SetEvent is called on the same handle.

What you did instead also makes sense, but the purpose of it is also pretty apparent, hard to understand how did you get confused: WaitForSingleObject(hThread, INFINITE) puts some other thread calling this method to wait for termination of hThread.

I'm not sure from your description if you tried to call in in the same thread having the handle hThread or not, but doing so would mean a thread waiting for itself to terminate, which would be, speaking politically correctly…, sorry, my thinking stops here… :-)

—SA
 
Share this answer
 
v2
Hello Sak what i pretended to do is use the sync APIs with the handle that createThread/AfxBeginThread/etc gave me, but when the thread exited the event didn't got signaled.

I have already fixed this, i was cleaning up this resource in dll detach process so i moved this code to a call before closing the dll and that fixed the problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900