suspendable/resumable/prioritizable task dispatcher
Hi, Unfortunately I don't have the vocabulary needed to ask this question, but hopefully I'll learn it here. Does Boost have something that will help me with this problem: I have a list of CPU-bound tasks I need to perform. I'll list two of them here: a) compute first zillion digits of pi (low priority) b) add two plus two (high priority) I co_spawn (a). Later, while (a) is running, I co_spawn (b). (a) will be suspended at a "co_suspension_point" so that (b) can take over. Of course when (b) completes, (a) will resume. For bonus points, it will support multiple threads (probably easy) and a task's priority can change over time (probably hard). Thank you! Chris PS: I was recently blown away by the coroutine capability of ASIO: Talking Async Ep1: Why C++20 is the Awesomest Language for Network Programming https://www.youtube.com/watch?v=icgnqFM-aY4
On 29/08/2022 01:58, chris-boostusers wrote:
I have a list of CPU-bound tasks I need to perform. I'll list two of them here:
a) compute first zillion digits of pi (low priority) b) add two plus two (high priority)
I co_spawn (a). Later, while (a) is running, I co_spawn (b). (a) will be suspended at a "co_suspension_point" so that (b) can take over. Of course when (b) completes, (a) will resume.
For bonus points, it will support multiple threads (probably easy) and a task's priority can change over time (probably hard).
Scheduling coroutines is in Boost.Fiber's wheelhouse. I don't think it provides a priority-based scheduler out of the box, but it does discuss it as a customisation point at: https://www.boost.org/doc/libs/1_80_0/libs/fiber/doc/html/fiber/scheduling.h...
On 8/28/22 4:06 PM, Gavin Lambert via Boost-users wrote:
Scheduling coroutines is in Boost.Fiber's wheelhouse. I don't think it provides a priority-based scheduler out of the box, but it does discuss it as a customisation point at
Gavin, Thank you. I had never heard of a "fiber" before but they are brilliant. Coroutines without coroutines! I ended up writing my own "algorithm" based on your suggestions. I had to overcome these issues: 1. When using multiple threads (to run fibers in parallel), the boost-provided algorithms busy-wait when there is no work to do. They have an option to suspend but they don't wake up to "steal" or "share" fibers from other threads. So my algorithm has a mechanism to wake up idle threads when excess work appears on a non-idle thread. 2. I added "priority" so a high priority can preempt a lower priority fiber 3. boost::fibers::yield() needed a modification to handle priorities. If the highest priority fiber calls yield(), and there are only lower priority fibers, control will be yielded to a lower priority fiber -- without input from the "algorithm". I would have preferred if the custom algorithm could have vetoed the context switch. But instead I wrote my own yield that consulted with the algo. This modification would have been easier if boost::fibers::scheduler::get_algo() were provided. Anyhow, thanks again. This is a wonderful library. Chris
On Sun, Aug 28, 2022 at 8:02 PM chris-boostusers--- via Boost-users
PS: I was recently blown away by the coroutine capability of ASIO: Talking Async Ep1: Why C++20 is the Awesomest Language for Network Programming https://www.youtube.com/watch?v=icgnqFM-aY4
Wow indeed! I had somehow missed that 1 year old talk myself too. Thanks for sharing. --DD
participants (3)
-
chris-boostusers@stankevitz.com
-
Dominique Devienne
-
Gavin Lambert