HOW TO:管理排程器執行個體

排程器執行個體可讓您為特定的排程原則與不同種類的工作負載建立關聯。本主題包含兩個基本範例,這些範例顯示如何建立和管理排程器執行個體。

這些範例建立的排程器會使用預設排程器原則。如需建立使用自訂原則之排程器的範例,請參閱 HOW TO:指定特定排程器原則

若要管理應用程式中的排程器執行個體

  1. 建立 concurrency::SchedulerPolicy 物件,其中包含的原則值若要使用排程器。

  2. 呼叫 concurrency::CurrentScheduler::Create 方法或 the concurrency::Scheduler::Create 方法,以建立排程器執行個體。

    如果您使用Scheduler::Create方法,請打 concurrency::Scheduler::Attach 方法,當您需要與目前內容相關排程器。

  3. 呼叫 CreateEvent 函式,為未發出信號、自動重設的事件物件建立控制代碼。

  4. 將控制代碼傳遞給剛建立的事件物件 concurrency::CurrentScheduler::RegisterShutdownEvent 方法或 the 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

請參閱

工作

HOW TO:指定特定排程器原則

概念

排程器執行個體