Last active
December 4, 2018 23:55
-
-
Save djarek/b9bc0e0502e586b0d66d3f5e96e86b2c to your computer and use it in GitHub Desktop.
post_hang.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct test_op | |
{ | |
using executor_type = boost::asio::io_context::executor_type; | |
executor_type get_executor() const noexcept | |
{ | |
return ctx1.get_executor(); | |
} | |
void operator()() | |
{ | |
while (i_ > 0) | |
{ | |
std::this_thread::sleep_for(std::chrono::seconds{1}); | |
--i_; | |
boost::asio::post(ctx2.get_executor(), std::move(*this)); | |
return; | |
} | |
} | |
boost::asio::io_context& ctx1; | |
boost::asio::io_context& ctx2; | |
// If you comment the work_guard out, the program hangs | |
boost::asio::executor_work_guard<executor_type> wg{ctx2.get_executor()}; | |
int i_ = 3; | |
}; | |
boost::asio::io_context ctx1{1}; | |
boost::asio::io_context ctx2{1}; | |
test_op{ctx1, ctx2}(); | |
std::thread t1{[&]() { ctx1.run(); }}; | |
std::thread t2{[&]() { ctx2.run(); }}; | |
t1.join(); | |
t2.join(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment