Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

Concurrency Runtime (CRT): The Task Scheduler


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 ScheduleGroupBase class.

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.

[Click image to view at full size]
Figure 8

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.

[Click image to view at full size]
Figure 9

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:

[Click image to view at full size]
Figure 10

So we can add a lightweight task to the group by invoking ScheduleGroupBase::ScheduleTask or Scheduler::ScheduleTask.

[Click image to view at full size]
Figure 11

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 m_workQueues field:

[Click image to view at full size]
Figure 12

So 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.

[Click image to view at full size]
Figure 13

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.

[Click image to view at full size]
Figure 14

The 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.

[Click image to view at full size]
Figure 15

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 class.

[Click image to view at full size]
Figure 16

Using 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 CreateThread API.

Unblocked Context queue.

The 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 m_runnableContexts.

Figure 17 shows some cases where the context is added to the runnable queue:

[Click image to view at full size]
Figure 17

So the context is added to the queue when it's unblocked or a virtual processor is retired from the Scheduler.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.