I'm trying to invoke boost::asio::spawn
twice to the same boost::asio::io_context::strand
, passing a coroutine each time and I was expecting the two coroutines to execute one after the other but they are instead executing in parallel. Here's code that illustrates this:
boost::asio::io_context ioc;
boost::asio::io_context::strand strand{ioc};
boost::asio::spawn(strand, [&](boost::asio::yield_context yield)
{
cout << "1\n";
ioc.post(yield);
cout << "2\n";
ioc.post(yield);
cout << "3\n";
});
boost::asio::spawn(strand, [&](boost::asio::yield_context yield)
{
cout << "10\n";
ioc.post(yield);
cout << "20\n";
ioc.post(yield);
cout << "30\n";
});
ioc.run();
This outputs:
1
10
2
20
3
30
When I was expecting:
1
2
3
10
20
30
In real code, the first coroutine sets up a socket (going through the motions of resolving/connecting/handshaking) and the second does a send/receive. My intent was to "append" the second coroutine to the strand and have it start executing only when the first one is done.
How can I achieve this effect?
Edit: More context. The first coroutine is in a constructor, the second in a member function. If I want to allow the user to write
Foo foo;
foo.bar();
how could I ensure the coroutine inside the constructor is finished before the one in bar() starts?