C++ C++;ReadConsoleInput不能与boost::thread一起使用
我创建了一个监听器类,它将调用在控制器对象上释放的方法,例如on_left_mouse_。它工作得很好,现在我正在尝试使用boost::thread在另一个线程中运行它。然而,我似乎做错了什么。我不熟悉多线程,所以这很容易是一个简单的错误 以下是从listener类中选择的部分:C++ C++;ReadConsoleInput不能与boost::thread一起使用,c++,windows,console,boost-thread,C++,Windows,Console,Boost Thread,我创建了一个监听器类,它将调用在控制器对象上释放的方法,例如on_left_mouse_。它工作得很好,现在我正在尝试使用boost::thread在另一个线程中运行它。然而,我似乎做错了什么。我不熟悉多线程,所以这很容易是一个简单的错误 以下是从listener类中选择的部分: void Listener::listen() { keepListening = true; while(keepListening) { if(timerEnabled) { th
void Listener::listen()
{
keepListening = true;
while(keepListening)
{
if(timerEnabled)
{
this->CheckForTimerEvent();
if( !PendingMouseOrKeyEvents()) //readconsoleinput is blocking
continue;
}
if(!keepListening) //could have been changed in a timer event
break;
if(!mouseEnabled && !keyboardEnabled)
continue;
ReadConsoleInput(hIn, &InRec, 1, &NumRead);
//see http://msdn.microsoft.com/en-us/library/windows/desktop/ms683499(v=vs.85).aspx
//for more information on InRec and its submembers
if(mouseEnabled &&InRec.EventType == MOUSE_EVENT)
{
this->ProcessMouseEvent(InRec.Event.MouseEvent);
cout << "here";
}
else if(keyboardEnabled && InRec.EventType == KEY_EVENT)
{
this->ProcessKeyEvent(InRec.Event.KeyEvent);
cout << "here";
}
}
}
void Listener::operator()()
{
listen();
}
void侦听器::listen()
{
keepListening=true;
同时(继续听)
{
if(timerenabed)
{
此->CheckForTimerEvent();
如果(!PendingMouseOrKeyEvents())//readconsoleinput被阻塞
继续;
}
如果(!keepListening)//可能在计时器事件中被更改
打破
如果(!mouseEnabled&!keyboard enabled)
继续;
读控制台输入(hIn、InRec、1和NumRead);
//看http://msdn.microsoft.com/en-us/library/windows/desktop/ms683499(v=vs.85).aspx
//有关InRec及其子成员的更多信息
if(mouseEnabled&&InRec.EventType==鼠标事件)
{
此->ProcessMouseeEvent(InRec.Event.MouseeEvent);
cout ProcessKeyEvent(InRec.Event.KeyEvent);
cout最有可能的情况是,您启动了线程,并忘记在退出测试程序之前等待线程退出。添加一个
listen.join();
在测试程序结束时。调用非UI线程是否安全?
快速查看文档并没有说明这两种方法,但这是您开始为Windows(或MacOSX&iOS)编写多线程软件时遇到的常见问题。这听起来很有可能。还有什么东西像这样受到限制?你能推荐一个关于这个的链接吗?这就成功了。谢谢!你能推荐一些好的多线程教程吗?@Kvothe“使用POSIX线程编程”来自David Buttenhof的书是一本非常好的入门书,即使它只涉及pthread。boost-thread与posix-threads类似。它非常有趣;-)