Does boost asio io_service guarantee execution of two parallel call chains?

百般思念 提交于 2019-12-31 05:17:08

问题


In my program, using boost asio io_service, I want to have two parallel call chains. Two endless loops writing and reading to two usb ports. But does boost asio io_service guarantee execution of two parallel call chains? Look at this minimal example:

#include <boost/asio/io_service.hpp>
#include <functional>

class Chain
{
public:
    Chain(boost::asio::io_service &io_service, const std::string &message)
        : io_service(io_service)
        , message(message)
    {}

    void call()
    {
        std::cout << message << std::endl;
        io_service.post(std::bind(&Chain::call, this));
    }

private:
    boost::asio::io_service &io_service;
    std::string message;
};

int main()
{
    boost::asio::io_service io_service;

    Chain chain1(io_service, "1");
    Chain chain2(io_service, "2");

    chain1.call();
    chain2.call();

    io_service.run();

    return 0;
}

It prints

1
2
1
2
1
...

because current io_service implementation is fifo dispatcher. Is it guarenteed in the future it will not prints

1
1
1
1
1
...

?


回答1:


The io_service currently makes no guarantees about the invocation order of handlers. Thus, the io_service could choose to invoke only a single call chain. Currently, only a strand specifies guarantees.

With that said, I would not worry too much about the io_service currently not making the guarantee. As Asio is on track of becoming the standard networking library, one can imagine that handler scheduling will be defined throughout the specification process. In particular, the io_service's usage of the proposed executors and schedulers should provide specific guarantees as to the scheduling behavior and its options.



来源:https://stackoverflow.com/questions/27733144/does-boost-asio-io-service-guarantee-execution-of-two-parallel-call-chains

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