如何:管理计划程序实例

通过使用计划程序实例,您可以将特定的计划策略与各种类型的工作负载相关联。 本主题包含两个基本示例,这些示例演示了如何创建和管理计划程序实例。

这些示例将创建使用默认计划程序策略的计划程序。 有关创建使用自定义策略的计划程序的示例,请参阅如何:指定特定的计划程序策略

管理应用程序中的计划程序实例

  1. 创建 concurrency::SchedulerPolicy 使用计划程序的对象,其中包含的策略值。

  2. 调用 concurrency::CurrentScheduler::Create 方法或 concurrency::Scheduler::Create 来创建的计划程序实例的方法。

    如果您使用Scheduler::Create方法,调用 concurrency::Scheduler::Attach 方法时需要调度程序相关联的当前上下文。

  3. 调用 CreateEvent 函数来创建非终止的自动重置事件对象的句柄。

  4. 将句柄传递到您刚才创建的事件对象 concurrency::CurrentScheduler::RegisterShutdownEvent 方法或 concurrency::Scheduler::RegisterShutdownEvent 方法。 这将注册要在销毁计划程序时设置的事件。

  5. 执行想要当前计划程序计划的任务。

  6. 调用 concurrency::CurrentScheduler::Detach 分离的当前计划程序并还原以前的计划程序与当前方法。

    如果您使用Scheduler::Create方法,调用 concurrency::Scheduler::Release 方法,以减少的引用计数的Scheduler对象。

  7. 将事件句柄传递给 WaitForSingleObject 函数以等待计划程序关闭。

  8. 调用 CloseHandle 函数以关闭事件对象的句柄。

示例

下面的代码演示了管理计划程序实例的两种方法。 每个示例首先均使用默认计划程序来执行一项打印出当前计划程序的唯一标识符的任务。 然后,每个示例使用计划程序实例再次执行相同任务。 最后,每个示例都将默认计划程序还原为当前计划程序并再次执行该任务。

第一个示例使用 concurrency::CurrentScheduler 来创建调度程序实例,并将其与当前上下文关联的类。 第二个示例使用 concurrency::Scheduler 类来执行相同的任务。 通常,CurrentScheduler 类用于处理当前计划程序。 如果您希望控制计划程序与当前上下文关联的时间,或者希望将特定计划程序与特定任务关联,则第二个示例(使用 Scheduler 类)很有用。

// scheduler-instance.cpp
// compile with: /EHsc
#include <windows.h>
#include <ppl.h>
#include <iostream>

using namespace concurrency;
using namespace std;

// Prints the identifier of the current scheduler to the console.
void perform_task()
{
   // A task group.
   task_group tasks;

   // Run a task in the group. The current scheduler schedules the task.
   tasks.run_and_wait([] { 
      wcout << L"Current scheduler id: " << CurrentScheduler::Id() << endl;
   });
}

// Uses the CurrentScheduler class to manage a scheduler instance.
void current_scheduler()
{
   // Run the task.
   // This prints the identifier of the default scheduler.
   perform_task();

   // For demonstration, create a scheduler object that uses 
   // the default policy values.
   wcout << L"Creating and attaching scheduler..." << endl;
   CurrentScheduler::Create(SchedulerPolicy());

   // Register to be notified when the scheduler shuts down.
   HANDLE hShutdownEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
   CurrentScheduler::RegisterShutdownEvent(hShutdownEvent);

   // Run the task again.
   // This prints the identifier of the new scheduler.
   perform_task();

   // Detach the current scheduler. This restores the previous scheduler
   // as the current one.
   wcout << L"Detaching scheduler..." << endl;
   CurrentScheduler::Detach();

   // Wait for the scheduler to shut down and destroy itself.
   WaitForSingleObject(hShutdownEvent, INFINITE);

   // Close the event handle.
   CloseHandle(hShutdownEvent);

   // Run the sample task again.
   // This prints the identifier of the default scheduler.
   perform_task();
}

// Uses the Scheduler class to manage a scheduler instance.
void explicit_scheduler()
{
   // Run the task.
   // This prints the identifier of the default scheduler.
   perform_task();

   // For demonstration, create a scheduler object that uses 
   // the default policy values.
   wcout << L"Creating scheduler..." << endl;
   Scheduler* scheduler = Scheduler::Create(SchedulerPolicy());

   // Register to be notified when the scheduler shuts down.
   HANDLE hShutdownEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
   scheduler->RegisterShutdownEvent(hShutdownEvent);

   // Associate the scheduler with the current thread.
   wcout << L"Attaching scheduler..." << endl;
   scheduler->Attach();

   // Run the sample task again.
   // This prints the identifier of the new scheduler.
   perform_task();

   // Detach the current scheduler. This restores the previous scheduler
   // as the current one.
   wcout << L"Detaching scheduler..." << endl;
   CurrentScheduler::Detach();

   // Release the final reference to the scheduler. This causes the scheduler
   // to shut down after all tasks finish.
   scheduler->Release();

   // Wait for the scheduler to shut down and destroy itself.
   WaitForSingleObject(hShutdownEvent, INFINITE);

   // Close the event handle.
   CloseHandle(hShutdownEvent);

   // Run the sample task again.
   // This prints the identifier of the default scheduler.
   perform_task();
}

int wmain()
{
   // Use the CurrentScheduler class to manage a scheduler instance.
   wcout << L"Using CurrentScheduler class..." << endl << endl;
   current_scheduler();

   wcout << endl << endl;

   // Use the Scheduler class to manage a scheduler instance.
   wcout << L"Using Scheduler class..." << endl << endl;
   explicit_scheduler();
}

该示例产生下面的输出。

Using CurrentScheduler class...

Current scheduler id: 0
Creating and attaching scheduler...
Current scheduler id: 1
Detaching scheduler...
Current scheduler id: 0


Using Scheduler class...

Current scheduler id: 0
Creating scheduler...
Attaching scheduler...
Current scheduler id: 2
Detaching scheduler...
Current scheduler id: 0

编译代码

将示例代码复制并将其粘贴在 Visual Studio 项目中,或将它粘贴到一个文件,名为计划程序 instance.cpp ,然后在 Visual Studio 命令提示符窗口中运行以下命令。

cl.exe /EHsc scheduler-instance.cpp

请参见

任务

如何:指定特定的计划程序策略

概念

计划程序实例