std::execution::run_loop
| Defined in header <execution>
|
||
class run_loop
{
public:
run_loop() noexcept;
run_loop(run_loop&&) = delete;
~run_loop();
/*run-loop-scheduler*/ get_scheduler() noexcept;
void run() noexcept;
void finish() noexcept;
};
|
(since C++26) | |
run_loop is a simple execution resource: a thread-safe first-in-first-out queue of work, and a loop that runs that work on the thread that calls run. Work is put into the queue through the scheduler returned by get_scheduler, from any thread.
A run_loop has no threads of its own: the program decides which thread drives it by calling run. This makes it a building block for event loops, for execution contexts that own a single thread, and for bringing work back to a particular thread, such as the main thread. It is also what this_thread::sync_wait uses to wait: the thread that calls sync_wait runs a run_loop until the sender completes.
Member functions
constructs a run_loop (public member function) | |
destroys the run_loop (public member function) | |
obtains a scheduler associated with the run_loop (public member function) | |
| runs queued work on the calling thread (public member function) | |
requests the run_loop to finish (public member function) |
Running and finishing the loop
runtakes work from the front of the queue and runs it on the calling thread, one item at a time, in the order in which it was scheduled. When the queue is empty, it blocks until more work arrives. It returns oncefinishhas been called and the queue is empty: work that was already queued whenfinishwas called is still run.finishcan be called from any thread, including from work running on the loop, and also beforerunis called. In that case,runruns whatever is queued and then returns right away. This happens insync_waitwhen the sender completes before the loop starts.- A
run_loopcan be run only once, by one thread. The behavior is undefined ifrunis called again after it has returned, or while another thread is running the loop, or iffinishis called more than once. - The destructor calls std::terminate if the loop is still running, or if there is work in the queue. Before destroying a
run_loop, callfinish, wait forrunto return (for example, by joining the thread that called it), and do not schedule more work. In a class that owns both arun_loopand the thread that runs it, declare therun_loopfirst, so that the thread is joined before the loop is destroyed. - All member functions other than
runand the destructor can be called from several threads at the same time without data races.
Associated scheduler
get_scheduler returns a scheduler of an unspecified type that schedules work onto the loop. Schedulers obtained from the same run_loop compare equal, and schedulers obtained from different ones compare unequal. A scheduler, and every sender obtained from it, can only be used while the run_loop exists.
The sender returned by execution::schedule for this scheduler puts its work at the back of the queue when it is started, and completes on the thread that runs the loop, when the work's turn comes:
- with
set_value(), or - with
set_stopped(), if stop has been requested on the receiver's stop token by the time the work's turn comes.
Scheduling cannot fail, so the sender never completes with an error. If the receiver's stop token can never be stopped, the sender can only complete with set_value(); this is what makes the scheduler usable with affine and task. Implementations are encouraged to make scheduling allocation-free, by using the started operation itself as the element of the queue.
Notes
- Work scheduled on a
run_loopruns only while some thread is insiderun. Work that is waited for by the only thread that could run the loop never runs, and the program hangs. - To wait until all work that uses the loop has finished, for example before calling
finish, start the work with spawn in a counting_scope and wait forjoin, as in the example below. A queue that is empty at some moment does not mean that no more work will be scheduled. - A sender that reads the scheduler from its environment with
read_env(get_scheduler), and is waited for withsync_wait, gets the scheduler ofsync_wait'srun_loop: work scheduled on it runs on the thread that calledsync_wait. - In earlier drafts, scheduling on a
run_loopcould fail with an exception, andfinishwas notnoexcept, to allow implementations based on a mutex and a condition variable. Implementations based on atomic operations, which cannot fail, made this unnecessary.
Example
A run_loop driven by a thread of its own backs a simple single-threaded execution context. Work scheduled on it runs on that thread in the order in which it was scheduled, and a counting_scope waits until it is all done.
The standard header is not available in the online compilers yet, so this link runs the example on Compiler Explorer with stdexec, the reference implementation.
#include <execution>
#include <iostream>
#include <thread>
class single_thread_context
{
// The loop is declared before the thread, so that the thread is joined
// (and run() has returned) before the loop is destroyed.
std::execution::run_loop loop_;
std::jthread worker_;
public:
single_thread_context()
: worker_([this] { loop_.run(); })
{}
~single_thread_context()
{
// run() returns once the work already queued has been done.
loop_.finish();
}
auto get_scheduler() noexcept
{
return loop_.get_scheduler();
}
};
int main()
{
namespace ex = std::execution;
single_thread_context ctx;
auto snd = ex::schedule(ctx.get_scheduler())
| ex::then([]
{
std::cout << "work runs on the loop\n";
return 42;
});
auto [value] = std::this_thread::sync_wait(std::move(snd)).value();
std::cout << value << '\n';
// Work runs in the order in which it was scheduled, and a counting_scope
// waits until all of it is done.
ex::counting_scope scope;
for (int i = 1; i <= 3; ++i)
{
auto task = [i]() noexcept { std::cout << "task " << i << '\n'; };
ex::spawn(ex::schedule(ctx.get_scheduler()) | ex::then(task),
scope.get_token());
}
std::this_thread::sync_wait(scope.join());
}
Output:
work runs on the loop
42
task 1
task 2
task 3
See also
(C++26) |
specifies that a type is a scheduler (concept) |
(C++26) |
obtains a sender that completes, with no values, on an execution agent of a given scheduler (customization point object) |
(C++26) |
blocks current thread until the specified sender completes and returns its async result (customization point object) |
(C++26) |
differs from simple_counting_scope by adding support for cancellation (class) |