[英]How to make WaitForSingleObject receive a signal inside a thread called from main as a class member function?
I have a working piece of code on main function in Windows with C++ like:我在 Windows 中使用 C++ 编写了一段关于 main 函数的工作代码,例如:
some_handle = dll_generate_handle;
while(true) {
if(WaitForSingleObject(some_handle,100)){
//CODE I AM INTERESTED ABOUT
}
}
The signal comes from a dll function.该信号来自一个 dll 函数。 I attempt to port this principle into a framework in the next manner:我尝试以下一种方式将此原则移植到框架中:
I have a main again but this main gets the static instance of a class i1.我又有一个 main ,但是这个 main 获取了类 i1 的静态实例。 This class has a member function "event_checking" doing the same piece of code above.这个类有一个成员函数“event_checking”,执行上面相同的代码。
I can access to this event_checking function through an instance i2 of an intermediate class so I decided to Wrap i1.event_checking with我可以通过中间类的实例 i2 访问此 event_checking 函数,因此我决定将 i1.event_checking 与
void eventCheckingWrapper(){ i1.event_checking(); }
Still in the main I spawn a thread on eventCheckingWrapper.仍然在主要我在 eventCheckingWrapper 上生成一个线程。 So I am not interested in the thread returning as long as signals arrive.所以我对只要信号到达就返回的线程不感兴趣。 An example of this principle could be:这个原则的一个例子可能是:
std::thread t(&ClassName::eventCheckingWrapper, &i2);
The thread is running well but no signals are arriving such that the timeout is always reached and the thread starts over in its while loop.线程运行良好,但没有信号到达,因此总是达到超时并且线程在其 while 循环中重新开始。
How can I properly get this to work?我怎样才能正确地让它工作?
I solved the issue.我解决了这个问题。 So everything Remy Lebeau said was true.所以雷米勒博说的都是真的。 In the end the dll had a function to activate the reception of signals through the event handle that I had to call before.最后,dll 有一个函数可以通过我之前必须调用的事件句柄来激活信号的接收。 So in reality no signals were coming but just to be informative what I could confirm after this debugging experience was basically that "parent" threads need to be kept alive in Windows, winapi WaitForSingleObject() keeps the state of an event signaled if it has not been waited for and new re-setting an event does not change its state (remains signaled) as long as no thread has waited for it (after which it changes to unsignaled mode) so time issues do not arise.所以实际上没有信号到来,但只是为了提供信息,我可以在这次调试体验之后确认基本上是“父”线程需要在 Windows 中保持活动状态,winapi WaitForSingleObject() 保持事件的状态,如果它没有一直等待并且重新设置事件不会改变其状态(保持有信号),只要没有线程等待它(之后它更改为无信号模式),因此不会出现时间问题。 As a matter of fact, it should be checked if the events provided by 3d party software care about the thread in which it was created and if therefore there is no problem where the handle is retrieved.事实上,应该检查 3d 方软件提供的事件是否关心创建它的线程,因此在检索句柄时是否没有问题。 Thanks!谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.