C++ ReadConsoleInput 不适用于 boost::thread
C++ ReadConsoleInput not working with boost::thread
>我创建了一个侦听器类,该侦听器类将在控制器对象上调用on_left_mouse_released等方法。它工作正常,现在我正在尝试使用 boost::thread 让它在另一个线程中运行。但是,我似乎做错了什么。我是多线程的新手,所以这很容易成为一个简单的错误。
以下是侦听器类中的选定部分:
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();
}
在我的主函数中,如果我创建一个名为 listener 的 Listener 对象,则说"listener();"这两个 cout 都与适当的事件一起发生。但是,如果我使用 "boost::thread listen (boost::ref(listener));",则什么都不会发生。
有人明白这是为什么吗?
很可能您已经启动了线程,但在退出测试程序之前忘记等待线程退出。添加一个
listen.join();
在测试程序结束时。
相关文章:
- 无法将参数传递给 boost::thread 构造函数
- 如何使用boost.thread运行多个接收器,每个线程应该分配给每个接收器?
- 使用 boost::thread 运行函子,但析构函数被错误地调用
- 最佳实践:我应该使用 std::thread、boost 还是本机调用
- boost::asio io_service thread pool with io_service::work
- 在thread.hpp上boost asio udp 螺纹= get_id()错误
- thread_specific_pointer可以在哪些平台上工作而无需 boost::thread?
- 将 boost::thread 与 C++11 std::mutex 混合使用是否安全?
- 同时使用 boost.python 和 boost.thread 时出现编译错误
- Boost.thread代码在Ubuntu和Windows中表现出不同的行为
- Pointer to boost::thread
- Linux上多核计算的低性能(openMP、boost::thread等)
- boost - thread.join() halts the ui
- 将基类的引用传递给 boost::thread 并在派生类中调用虚函数是否有效
- Difference between boost::thread and std::thread
- std::thread是boost::thread的替代品吗?
- 将 boost/thread headers 与 clang++ (Windows) 一起使用时出现问题
- 在linux下使用boost::thread创建一个boost::asio工作线程
- "Thread" Boost 无法在 OSX 上构建?
- C++ Boost::Thread & Boost::ASIO Memory leak