【发布时间】:2014-12-17 13:09:54
【问题描述】:
我正在尝试将 boost 信号与 asio 混合以执行基于调度的处理程序调用。当从线程调用 post 方法时,io_service::run 立即退出,处理 post 的回调永远不会被调用,回调是 C++11 lambda 例程。我正在粘贴代码以进行更多分析。
#include<iostream>
#include<thread>
#include<boost/signals2/signal.hpp>
#include<boost/asio.hpp>
static boost::asio::io_service svc;
static boost::signals2::signal<void(std::string)> textEntered;
static void
handleInputText(std::string text)
{
std::cout<<"handleInputText()"<<" text provided: "<<text;
return;
}
static void
worker()
{
sleep(2);
svc.post([](){
std::cout<<"\nRaising signal.";
std::string hello("hello world");
textEntered(hello);
});
return;
}
int main(int ac, char **av)
{
try
{
textEntered.connect(&handleInputText);
std::thread w(std::bind(&worker));
svc.run();
w.join();
}
catch(std::exception &ex)
{
std::cerr<<"main() exited with exception:"<<ex.what();
}
return 0;
}
【问题讨论】:
标签: c++11 boost boost-asio