Skip to content

ui::Scheduler

Header: winui/core/Scheduler.hpp.

A pool of worker threads for background work and parallel layout.

Construct one to give a piece of work its own threads, and hand it to ui::on_background(pool) or to ParallelLayout::scheduler. Work on a pool must not touch the UI tree. The Inline mode runs everything synchronously on the calling thread, for deterministic tests. The destructor runs every job still queued, then joins the workers.

Example:

// Two threads of its own, so an index never waits behind other background work.
ui::Scheduler indexers{2};

// `status` is app state that outlives the window, shown by a Text bound to it.
ui::FireAndForget index(ui::Scheduler& indexers, ui::Window& window, ui::Property<std::string>& status,
                        std::filesystem::path folder)
{
    const ui::WindowRef home = window.ref();
    co_await ui::on_background(indexers); // on one of the pool's threads
    const auto files = std::distance(std::filesystem::recursive_directory_iterator{folder}, {});
    co_await ui::on_ui_thread(home);
    status = std::format("{} files", files);
}

Scheduler

Scheduler(std::size_t thread_count = 0)

Real worker threads. thread_count == 0 auto-detects: max(1, hardware_concurrency()).

Scheduler

Scheduler(Inline)

The deterministic mode (see Inline): no threads.

~Scheduler

~Scheduler()

Runs every job still queued, then joins the worker threads.

Scheduler

Scheduler(const Scheduler&) = delete

Not copyable: it owns threads.

operator=

Scheduler& operator=(const Scheduler&) = delete

Not copyable: it owns threads.

thread_count

std::size_t thread_count() const

Real worker thread count (0 in inline mode).

worker_count

std::size_t worker_count() const

How many jobs can run at once: thread_count(), and 1 in inline mode. Parallel layout splits its measuring into this many chunks.

struct Inline

Tag selecting the deterministic mode for tests: work runs synchronously, in the order it was queued, on the calling thread, with no worker threads and no interleaving. Construct with Scheduler{Scheduler::Inline{}}.