Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
51 lines (32 sloc) 3.55 KB
title helpviewer_keywords ms.assetid
How to: Create Agents that Use Specific Scheduler Policies
scheduler policies, agents [Concurrency Runtime]
creating agents that use specific policies [Concurrency Runtime]

How to: Create Agents that Use Specific Scheduler Policies

An agent is an application component that works asynchronously with other components to solve larger computing tasks. An agent typically has a set life cycle and maintains state.

Every agent can have unique application requirements. For example, an agent that enables user interaction (either retrieving input or displaying output) might require higher priority access to computing resources. Scheduler policies let you control the strategy that the scheduler uses when it manages tasks. This topic demonstrates how to create agents that use specific scheduler policies.

For a basic example that uses custom scheduler policies together with asynchronous message blocks, see How to: Specify Specific Scheduler Policies.

This topic uses functionality from the Asynchronous Agents Library, such as agents, message blocks, and message-passing functions, to perform work. For more information about the Asynchronous Agents Library, see Asynchronous Agents Library.


The following example defines two classes that derive from concurrency::agent: permutor and printer. The permutor class computes all permutations of a given input string. The printer class prints progress messages to the console. The permutor class performs a computationally-intensive operation, which might use all available computing resources. To be useful, the printer class must print each progress message in a timely manner.

To provide the printer class fair access to computing resources, this example uses steps that are described in How to: Manage a Scheduler Instance to create a scheduler instance that has a custom policy. The custom policy specifies the thread priority to be the highest priority class.

To illustrate the benefits of using a scheduler that has a custom policy, this example performs the overall task two times. The example first uses the default scheduler to schedule both tasks. The example then uses the default scheduler to schedule the permutor object, and a scheduler that has a custom policy to schedule the printer object.


This example produces the following output.

With default scheduler:
Computing all permutations of 'Grapefruit'...
100% complete...

With higher context priority:
Computing all permutations of 'Grapefruit'...
100% complete...

Although both sets of tasks produce the same result, the version that uses a custom policy enables the printer object to run at an elevated priority so that it behaves more responsively.

Compiling the Code

Copy the example code and paste it in a Visual Studio project, or paste it in a file that is named permute-strings.cpp and then run the following command in a Visual Studio Command Prompt window.

cl.exe /EHsc permute-strings.cpp

See also

Scheduler Policies
Asynchronous Agents

You can’t perform that action at this time.