c++ - Is it safe to use spawn directly in an asio stackful coroutine? -


when use spawn start new stackfull coroutine in coroutine, valgrind says lot of using uninitialised value(valgrind output).

then use io_service.post invoke handler,and start new stackfull coroutine in it, every thing seems fine.

i have searched , read documents, can't find how create new stackfull coroutine safely in stackfull coroutine.

here code:

#include <iostream> #include <boost/asio.hpp> #include <boost/asio/spawn.hpp> #include <boost/asio/system_timer.hpp> #include <chrono>  using namespace std;  int main() {     auto use_post = false;     boost::asio::io_service io_service;     boost::asio::spawn(io_service,                        [&io_service, &use_post](boost::asio::yield_context yield){         if(use_post){             io_service.post([&io_service]{                 boost::asio::spawn(io_service, [&io_service](boost::asio::yield_context yield){                     boost::asio::system_timer timer(io_service);                     timer.expires_from_now(std::chrono::seconds(1));                     timer.async_wait(yield);                     cout << "sleep 1 second" << endl;                 });             });         }         else{             boost::asio::spawn(io_service, [&io_service](boost::asio::yield_context yield){                 boost::asio::system_timer timer(io_service);                 timer.expires_from_now(std::chrono::seconds(1));                 timer.async_wait(yield);                 cout << "sleep 1 second" << endl;             });         }         boost::asio::system_timer timer(io_service);         timer.expires_from_now(std::chrono::seconds(2));         timer.async_wait(yield);         cout << "sleep 2 seconds" << endl;     });     io_service.run();     return 0; } 

set true use_post variable, new stackfull coroutine started post + spawn.

maybe don't read documents carefully, can't find thing usefull in boost.asio c++ network programming, n4045 , boost asio document.

it safe.

boost.asio's first-class support boost.coroutine thin facade 2 notable behaviors:

  • a coroutine , handlers resume use strand execution context. guarantees coroutine not resumed before has yielded.
  • boost.asio prevents coroutine staying indefinitely suspended if detects there no handlers available resume it. when occurs, boost.asio destroy coroutine, causing suspended stack unwind. see this answer more details.

in example code above, spawn(io_service&) overload causes resulting coroutine have own strand. result, if multiple threads running io_service, each of coroutines may run in parallel, not guaranteed so. on other hand, if 1 uses spawn(yield_context) overload, new coroutine have same execution context (i.e strand) calling coroutine, preventing parallel execution.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -