boost asio post不工作​​,io_service :: run后post [英] boost asio post not working , io_service::run exits right after post

查看:817
本文介绍了boost asio post不工作​​,io_service :: run后post的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用asio混合boost信号来执行基于分派的处理程序调用。当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;
return;
}

static void
worker()
{
sleep(2);
svc.post([](){
std :: cout<<\\\
Raising 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()退出with exception:<什么();
}
return 0;
}


解决方案



您开始一个可以最终发布工作的主题,但主线程已经退出。 / p>

要么在线程上运行ioservice,要么确保它具有 io_service :: work



这里有一个专用的服务线程和工作项目的修复:



Live On Coliru

 #include< boost / asio.hpp> 
#include< iostream>
#include< boost / asio.hpp>
#include< boost / signals2.hpp>
#include< boost / thread.hpp>
#include< boost / make_shared.hpp>

static boost :: asio :: io_service svc;
static boost :: shared_ptr& boost :: asio :: io_service :: work> work_lock;

static boost :: signals2 :: signal< void(std :: string)> textEntered;

static void
handleInputText(std :: string text)
{
std :: cout<<handleInputText()<< :<< text;
return;
}

static void
worker()
{
sleep(2);
svc.post([](){
std :: cout<<\\\
Raising signal。;
std :: string hello(hello world);
textEntered(hello);
});
return;
}

int main()
{
try
{
work_lock = boost :: make_shared& boost :: asio :: io_service :: work>(svc);
textEntered.connect(& handleInputText);

boost :: thread_group tg;
tg.create_thread(boost :: bind(& boost :: asio :: io_service :: run,& svc));
tg.create_thread(& worker);

boost :: this_thread :: sleep_for(boost :: chrono :: seconds(3));
work_lock.reset();

tg.join_all();
}
catch(std :: exception& ex)
{
std :: cerr<<main()退出with exception:<什么();
}
return 0;
}

列印:

 提高signal.handleInputText()文本提供:hello world 


I am trying to mix boost signals with asio to do a dispatch based handler invocation. when the post method is invoked from a thread the io_service::run exits immediately, the callback handled to post is never invoked, callback is a C++11 lambda routine. I am pasting the code for more analysis.

#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;
}

解决方案

You don't actually post any work to the service.

You start a thread that may eventually post work, but the main thread has already exited by that time.

Either, run the ioservice on the thread or make sure it has io_service::work

Here's a fix with a dedicated service thread and a work item:

Live On Coliru

#include<boost/asio.hpp>
#include<iostream>
#include<boost/asio.hpp>
#include<boost/signals2.hpp>
#include<boost/thread.hpp>
#include<boost/make_shared.hpp>

static boost::asio::io_service svc;
static boost::shared_ptr<boost::asio::io_service::work> work_lock;

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()
{
    try
    {
        work_lock = boost::make_shared<boost::asio::io_service::work>(svc);
        textEntered.connect(&handleInputText);

        boost::thread_group tg;
        tg.create_thread(boost::bind(&boost::asio::io_service::run, &svc));
        tg.create_thread(&worker);

        boost::this_thread::sleep_for(boost::chrono::seconds(3));
        work_lock.reset();

        tg.join_all();
    }
    catch(std::exception &ex)
    {
        std::cerr<<"main() exited with exception:"<<ex.what();
    }
    return 0;
}

Prints:

Raising signal.handleInputText() text provided: hello world

这篇关于boost asio post不工作​​,io_service :: run后post的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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