我无法在dll中获得线程句柄获取信号,但是在exe中却获得了相同的代码 [英] I can'get a thread handle get signal in a dll, but the same code gets signal in an exe

查看:66
本文介绍了我无法在dll中获得线程句柄获取信号,但是在exe中却获得了相同的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

怎么了?一切.

不清楚您要实现什么,但不是很明显:调用SetEvent(hEvent)来唤醒具有相同事件句柄的名为WaitForSingleObject(hEvent, INFINITE)的其他线程吗?以后发生的情况取决于事件的初始化方式:如果它是自动重置的,则在等待线程唤醒时它将重置,否则它将保持设置状态.

顺便说一下,自动重置提供了非平凡的效果,这是其他事物和逻辑运算符无法模拟的:重置是 atomic ,它被设计为一次仅传递一个线程并保持处于等待状态的任意数量的其他等待线程,直到在同一句柄上调用另一个SetEvent.

相反,您所做的事情也很有意义,但是它的目的也很明显,很难理解您是如何困惑的:WaitForSingleObject(hThread, INFINITE)放置其他线程调用此方法以等待hThread.

从您的描述中我不确定您是否尝试在具有句柄hThread的同一个线程中进行调用,但是这样做将意味着线程在等待自身终止,即从政治上讲正确…,对不起,我的想法就在这里... :-)

—SA
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


您好Sak我假装要做的是将同步API与createThread/AfxBeginThread/etc给我的句柄一起使用,但是当线程退出时事件未发出信号.

我已经解决了这个问题,我正在dll分离过程中清理此资源,因此我在关闭dll之前将这段代码移到了调用中,从而解决了该问题.
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.


这篇关于我无法在dll中获得线程句柄获取信号,但是在exe中却获得了相同的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆