我正在使用asio :: spawn启动一些协同程序,我想等到所有这些都完成后再做其他工作。怎么做?
控制流程如下:
asio::spawn (io, [] (asio::yield_context yield) {
...
// starting few coroutines
asio::spawn (yield, [] (asio::yield_context yield2) { ... });
asio::spawn (yield, [] (asio::yield_context yield2) { ... });
asio::spawn (yield, [] (asio::yield_context yield2) { ... });
asio::spawn (yield, [] (asio::yield_context yield2) { ... });
// now I want to wait for all of them to finish before I do
// some other work?
...
});
io.run ();
更新
以下是示例代码
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/steady_timer.hpp>
#include <chrono>
#include <iostream>
using namespace std;
int main ()
{
using namespace boost::asio;
io_service io;
spawn (io, [&] (yield_context yield) {
cout << "main coro starts\n";
auto lambda = [&] (yield_context yield)
{
cout << "in lambda inside subcoroutine - starts\n";
steady_timer t (io, std::chrono::seconds (1));
t.async_wait (yield);
cout << "in lambda inside subcoroutine - finishes\n";
};
// starting few coroutines
spawn (yield, lambda);
spawn (yield, lambda);
// now I want to wait for all of them to finish before I do
// some other work?
// ???
cout << "main coro finishes\n";
});
io.run ();
}
输出是:
// main coro starts
// in lambda inside subcoroutine - starts
// in lambda inside subcoroutine - starts
// main coro finishes <----
// in lambda inside subcoroutine - finishes
// in lambda inside subcoroutine - finishes
虽然我期待:
// main coro starts
// in lambda inside subcoroutine - starts
// in lambda inside subcoroutine - starts
// in lambda inside subcoroutine - finishes
// in lambda inside subcoroutine - finishes
// main coro finishes
(参见&#34;主要coro饰面和#34;行的地方)
答案 0 :(得分:1)
我找到了......一种解决方法。
我可以使用具有无限持续时间的计时器,并从最后的子协程中取消它。这将唤醒主协程。
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/steady_timer.hpp>
#include <iostream>
using namespace std;
int main ()
{
using namespace boost::asio;
io_service io;
spawn (io, [&] (yield_context yield) {
cout << "main coro starts\n";
steady_timer rendez_vous (io, steady_timer::clock_type::duration::max ());
/* volatile */ int counter = 2;
auto lambda = [&] (yield_context yield)
{
cout << "in lambda inside subcoroutine - starts\n";
steady_timer t (io, boost::chrono::seconds (1));
t.async_wait (yield);
cout << "in lambda inside subcoroutine - finishes\n";
if (--counter == 0)
rendez_vous.cancel ();
};
// starting few coroutines
spawn (yield, lambda);
spawn (yield, lambda);
// now I want to wait for all of them to finish before I do
// some other work?
// ???
boost::system::error_code ignored_ec;
rendez_vous.async_wait (yield [ignored_ec]);
// ignore errors here by reason.
cout << "main coro finishes\n";
});
io.run ();
}
坦率地说,我不喜欢这个解决方案,因为它滥用“计时器”概念和对象,这可能会浪费系统资源。
答案 1 :(得分:1)
更好的选择是使用光纤(boost.fiber集成到boost.asio中)。 boost :: fiber是一个协程+调度程序+同步类(像std :: thread这样的API),可以像coroutines一样在boost.asio上下文中使用。