Timers
On this page
Everything time-related in ConcurrentTasks runs on the core's timer thread. It is off until you enable it.
Enabling timers
EnableTimers starts the timer thread; DisableTimers takes it back. The calls are counted, so a library and the application can both enable timers.
When the last DisableTimers runs, everything still waiting on a timer ends: tasks scheduled to
start later exit without running, sleeping tasks wake up, waits with a timeout time out.
Important
Without timers, waits with a timeout wait without limit, SleepTask returns
false at once, and tasks cannot be started later. Enable timers before you rely on any of them.
Durations
A Duration counts units of 100 nanoseconds on every platform. Build one with Microseconds, Milliseconds or Seconds; Infinite means no limit.
A duration says how long at least. On Windows the timer thread uses a high-resolution waitable timer and fires on a grid of about 0.5 ms on Windows 10 version 1803 and later (otherwise on the system tick, about 15.6 ms). On Linux it waits for an absolute deadline of the monotonic clock, on the kernel's high-resolution timers, without a fixed grid. Either way a task may start running a little later than that.
What timers do
| What | How |
|---|---|
| Sleep | SleepTask or Task::SleepFor suspend the calling task for a while. Only a CancelTask cuts a sleep short. |
| Wait with a timeout | Wait(Duration) on the _s primitives, for example Semaphore_s; underneath SuspendTaskFor. |
| Start a task later | Task::Schedule(priority, delay) or AddTimedTask. Calling it again before the start moves the start. |
| Cancel a later start | Task::Cancel / CancelTask — the task exits without running. |
| Start now instead | Task::ScheduleNow / ScheduleTimedTask. |
A task that is started later must provide control storage
(GetControlData); Task does. The TaskSwap scheduler
cannot start tasks later.
Cancelling needs no timers: a CancelTask also ends a wait without a time limit
(Wait(Duration::Infinite())) when timers are off.