[asio-users] Waiting on pending work
Brought to you by:
chris_kohlhoff
From: Rory J. <rs...@gm...> - 2022-02-22 00:53:45
|
I found this old thread that asked a similar question, but wanted to bring it up again: https://sourceforge.net/p/asio/mailman/message/24964454/ In my application, there is a single io_context set up that has two threads, and is held open by adding another executor by: asio::any_io_executor work_ {asio::require(io_context_.get_executor(), asio::execution::outstanding_work.tracked)}; Then, in individual classes, I use asio::make_strand to derive individual strands from the single io_context, and work gets assigned through the strands. At each class's shutdown, I want to make sure the work is completed before destroying the class. Currently, I use a std::binary_semaphore, acquire the semaphore when work first starts, then release the semaphore when the work cycle exits, whether it exits by a socket error, finish signal, or exception. Then I acquire the semaphore in the destructor, which forces the destructor to wait for the semaphore to be released. The main application is responsible for terminating work_ and the io_context, which occurs after the individual classes destroy themselves. This works fine, but is messy and requires careful analysis of the code to make sure I acquire and release the semaphore correctly in all possible sequences of events. It would be much easier to set up a wait on all work done by the thread, but I do not see a way to do so. Is there some method of setting up a wait that I'm not aware of? |