OpenEvent和多线程 [英] OpenEvent and multithreading

查看:167
本文介绍了OpenEvent和多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个c ++代码,我在按下按钮时创建一个线程。当我推两次我有多线程并且我试验一个线程如何等待WaitForSingleObject()应用于一个Event对象。

如果我将Event对象定义为静态,构造函数中的CreateEvent()方法和OpenEvent在实现线程的函数的开头,一切似乎都没问题。

但是如果我在其他地方定义Event对象,非静态,似乎OpenEvent在函数的开头虽然OpenEvent返回非null,但实现该线程的线程不能正常工作并且线程不会等待另一个线程。



HANDLE hevent0;

hevent0 = CreateEvent(NULL,TRUE,TRUE,_T(hevenim));

...



HANDLE hevent;

hevent = OpenEvent(EVENT_ALL_ACCESS,TRUE,_T(hevenim));

rwait = WaitForSingleObject(hevent,9999);



我的问题是为什么OpenEvent在第二种情况下无法正常工作?

I have a c++ code where I create a thread when I push a button. When I push twice I have multithreading and I experiment how a thread waits for another by WaitForSingleObject() which apply on an Event object.
If I define the Event object as static, CreateEvent() in the constructor method, and OpenEvent at the begining of the function that implements the thread, all seems to be OK.
But if I define the Event object elsewhere, nonstatic, seems that the OpenEvent at the begining of the function that implements the thread doesn’t work properly and the thread doesn’t wait one for another,although the OpenEvent returns non null.

HANDLE hevent0;
hevent0=CreateEvent(NULL,TRUE,TRUE,_T("hevenim"));


HANDLE hevent;
hevent=OpenEvent(EVENT_ALL_ACCESS,TRUE, _T("hevenim"));
rwait=WaitForSingleObject(hevent,9999);

My question is why the OpenEvent doesn’t work properly in the second case?

推荐答案

每个线程都有自己的值。因此,您需要一个全局或静态事件来进行同步,或者每个线程都在做自己的业务。
Every thread has its own values. So you need a global or static event for syncronizing, or every thread is doing its own business.


这篇关于OpenEvent和多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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