Manage Task Queues. When Scheduler is created, tasks can be assigned to it to be
executed. The Scheduler store tasks into queues.to enforce the cohesion of classes, the queues are not managed directly by the
ThreadScheduler class but by
A schedule group affinitizes, or groups, related tasks together. Every Scheduler has one or more schedule groups. Use schedule groups when you require a high degree of locality among tasks, for example, when a group of related tasks benefit from executing on the same processor node.
In Figure 8, the runtime provides two kinds of
ScheduleGroup: FairScheduleGroup and
CacheLocalScheduleGroup, choosing between those two groups will be explained later.
Every Scheduler has a default schedule group for every scheduling node.The runtime creates one scheduling node for every processor package or Non-Uniform Memory Architecture (NUMA) node. If you do not explicitly associate a task with a schedule group, the Scheduler chooses which group to add the task to.
In Figure 9, the
schedulingRing is responsible for managing Scheduler Groups, it contains a list of groups and creates them.
The Schedule group contains three kind of queues:
- FIFO Queue
- Work-stealing Queue
- Unblocked Queue
The FIFO Queue contains lightweight tasks. A lightweight task resembles the function that you provide to the Windows API
CreateThread function. Therefore, lightweight tasks are useful when you adapt existing code to use the scheduling functionality of the CRT.
A lighweight task is represented by
RealizedChore class, and the FIFO queue of the schedule group is represented by the field
m_realizedChores. Let's search all methods using directly this queue:
So we can add a lightweight task to the group by invoking
Here is an interesting article about lightweight tasks.
The Work-stealing Queue.While there's only one FIFO queue associated to the schedule group, but the schedule group reference a list of work sealing queues, for each worker thread there's a local queue associated to it.
A thread that is attached to a scheduler is known as an "execution context", or just "context", so actually this local queue is associated to
Context class. The
Context class provides a programming abstraction for an execution context and offer the ability to cooperatively block, unblock, and yield
the current context.
And to be sure that only
Context creates this kind of queue, you can directly access the
Context is responsible for creating this queue, and for each context there's a local work-stealing queue associated to it. To illustrate the behavior of the work-stealing algorithm, imagine
that you have two worker threads allocated to the scheduler. As explained before, for each worker thread there's a local queue associated with it.
Imagine that you have three tasks in the queue of the Worker Thread 1 -- Tasks 3 and 4 are waiting to be executed while Task 5 is running.
Dispatch method finds that there is nothing in the queue, so Task 3 is moved, or "stolen" from its original queue, to be distributed to the worker thread available.
How can you create a task managed by work-stealing queue?
To answer this question, let's examine the methods indirectly invoking the
CreateWorkQueue method. As shown in Figure 16, this kind of task could be created by using the
task_group to add a new task is better than using
Sheduler::ScheduleTask to create lightweight tasks; indeed the work-stealing algorithm optimizes better the using of virtual processors allocated to the Scheduler. However,
ScheduleTaskcould be better to migrate easily from the existing code using the
Unblocked Context queue.
Context class lets you block or yield the current execution context. Blocking or yielding is useful when the current context cannot continue because a resource is not available. The
Context::Block method blocks the current context. A context that is blocked yields its processing resources so that the runtime can perform other tasks. The
Context::Unblock method unblocks a blocked context.
When a context is unblocked and it's available to be executed, it's added to the runnable context queue. This queue is represented by the field
Figure 17 shows some cases where the context is added to the runnable queue:
So the context is added to the queue when it's unblocked or a virtual processor is retired from the Scheduler.