[asio][coroutine] Intentional synchronization of a single coroutine
Within a large asynchronous project, I have a coroutine that needs to
synchronize with other calls to that coroutine. This is a very simplified
(and somewhat silly) example:
#include <iostream>
#include <cstdlib>
#include <chrono>
#include
Em sex, 9 de ago de 2019 às 16:12, Jared McIntyre via Boost < boost@lists.boost.org> escreveu:
What I need is a way for each call to bottleneck() to fully complete before the next one starts, and each call occurs in the order they were called. This would make the output 3 2 1 0. I also want to allow other asynchronous routines on the io_service to run when the coroutine is yielding (so no cheating and turning the delay timer into a sleep).
Take a look at this project: https://github.com/blinktrade/iofiber It should solve your needs. You could... like: spawn(ioservice, [](fiber::this_fiber this_fiber) { spawn(this_fiber, std::bind(&bottleneck, 3s, _1)) .join(this_fiber); spawn(this_fiber, std::bind(&bottleneck, 2s, _1)) .join(this_fiber); spawn(this_fiber, std::bind(&bottleneck, 1s, _1)) .join(this_fiber); spawn(this_fiber, std::bind(&bottleneck, 0s, _1)) .join(this_fiber); }) .detach(); With IOFiber, spawn() returns a fiber handle that you can use to join() the fiber. The handle is moveable and then you can pass it around. Check the documentation. -- Vinícius dos Santos Oliveira https://vinipsmaker.github.io/
participants (3)
-
Bjorn Reese
-
Jared McIntyre
-
Vinícius dos Santos Oliveira