shared-io-context usage problem.
pata00 opened this issue · 2 comments
pata00 commented
the problem is :
if the io_context
has not used before agrpc::run
, asio::post(io_context, cb)
from another thread, cb can't be called...
void test_func(){
asio::io_context io_context{1};
example::v1::Example::Stub stub{grpc::CreateChannel(host, grpc::InsecureChannelCredentials())};
agrpc::GrpcContext grpc_context{std::make_unique<grpc::CompletionQueue>()};
asio::post(io_context,
[&]
{
auto worker = asio::make_work_guard(io_context);// without this line , asio::post(io_context, cb) from another thread can't work....
io_context.get_executor().on_work_finished();
agrpc::run(grpc_context, io_context);
io_context.get_executor().on_work_started();
});
io_context.run();
}
Tradias commented
Yes that is expected. As you have seen in the examples, they both co_spawn a task onto the io_context before agrpc::run
. The manual on_work_started/finished
is only there to make agrpc::run
return once both the io_context and GrpcContext have run out of work. If you have a different stop condition then you can also specify it directly and ignore the work tracking:
bool stopped{false}; // your custom stop condition
auto io_context_guard = asio::make_work_guard(io_context);
auto grpc_context_guard = asio::make_work_guard(grpc_context);
asio::post(io_context,
[&]
{
agrpc::run(grpc_context, io_context, [] { return stopped; });
});
io_context.run();
pata00 commented
Got it, After change code like below, Everything Is OK. :)
asio::post(io_context, [&] {
agrpc::run(grpc_context, io_context, [&] {
return grpc_context.is_stopped();
});
});
io_context.run();