boost asio post not working , io_service::run exits right after post

*爱你&永不变心* 提交于 2020-01-11 11:09:08

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/27526352/boost-asio-post-not-working-io-servicerun-exits-right-after-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!