屈服于Boost.Asio Stackful Coroutine

时间:2014-09-30 18:17:36

标签: c++ c++11 boost boost-asio coroutine

当使用Boost.Asio堆栈协同程序时,我怎样才能"手动"屈服,以便另一个协程或异步操作有机会运行?例如,我需要在对从TCP套接字收到的命令发送响应之前执行长计算:

asio::spawn(strand_, [this, self](asio::yield_context yield)
{
    char data[256];
    while (socket_.is_open())
    {
        size_t n = socket_.async_read_some(boost::asio::buffer(data),
                                           yield);

        if (startsWith(data, "computePi"))
        {
            while (!computationFinished)
            {
                computeSomeMore();
                yield; // WHAT SHOULD THIS LINE BE?
            }

            storeResultIn(data);
            boost::asio::async_write(socket_, boost::asio::buffer(data, n),
                                     yield);
        }
    }
});

2 个答案:

答案 0 :(得分:4)

您只需在io_service对象上调用poll_one()即可。

完整的工作样本:

#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/thread.hpp>
#include <iostream>

namespace asio = boost::asio;
using boost::asio::ip::tcp;
using std::begin;
using std::end;

bool computationFinished = false;
void computeSomeMore() {
    static int count = 0;
    if (count++>10)
    {
        computationFinished = true;
        std::cout << "Calculation finished\n";
    } else
    {
        std::cout << "Calculating...\n";
        boost::this_thread::sleep_for(boost::chrono::milliseconds(200));
    }
}

template <typename T> void storeResultIn(T& a) {
    std::fill(begin(a), end(a), '4');
}

int main()
{
    asio::io_service iosvc;
    tcp::socket s(iosvc);
    tcp::resolver r(iosvc);

    tcp::acceptor a(iosvc, tcp::endpoint(tcp::v4(), 6767));

    a.accept(s);
    {
        asio::spawn(iosvc, [&iosvc,&s](asio::yield_context yield)
        {
            char data[256];
            while (s.is_open())
            {
                size_t n = s.async_read_some(boost::asio::buffer(data), yield);

                if (boost::algorithm::starts_with(data, "computePi"))
                {
                    iosvc.post([]{std::cout << "I can still breath\n";}); // some demo work
                    iosvc.post([]{std::cout << "And be responsive\n";});

                    while (!computationFinished)
                    {
                        computeSomeMore();
                        iosvc.poll_one(); // this enables the demo work to be run
                    }

                    storeResultIn(data);
                    boost::asio::async_write(s, boost::asio::buffer(data, n), yield);
                } else
                {
                    std::cout << "Received unknown command '" << std::string(data, data+n) << "'\n";
                }
            }
        });
    }

    iosvc.run();
    std::cout << "Bye bye\n";
}

当发送“computePi”时,服务器打印:

Calculating...
I can still breath
Calculating...
And be responsive
Calculating...
Calculating...
Calculating...
Calculating...
Calculating...
Calculating...
Calculating...
Calculating...
Calculating...
Calculation finished

答案 1 :(得分:4)

这比你想象的要简单:

iosvc.post(yield);

会做到这一点。

iosvc借用@ sehe的示例代码)