如何:管理排程器執行個體
排程器執行個體可讓您為特定的排程原則與不同種類的工作負載建立關聯。 本主題包含兩個基本範例,這些範例顯示如何建立和管理排程器執行個體。
這些範例建立的排程器會使用預設排程器原則。 如需建立使用自訂原則之排程器的範例,請參閱 如何:指定特定排程器原則。
若要管理應用程式中的排程器執行個體
建立 concurrency::SchedulerPolicy 物件,這個物件包含要供排程器使用的原則值。
呼叫 concurrency::CurrentScheduler::Create 方法或 concurrency::Scheduler::Create 方法,以建立排程器執行個體。
如果您使用 Scheduler::Create 方法,則當您需要將排程器與目前內容產生關聯時,請呼叫 concurrency::Scheduler::Attach 方法。
呼叫 CreateEvent 函式,為未發出信號、自動重設的事件物件建立控制代碼。
將您剛才建立之事件物件的控制代碼傳遞給 concurrency::CurrentScheduler::RegisterShutdownEvent 方法或 concurrency::Scheduler::RegisterShutdownEvent 方法。 這會在排程器被終結時註冊此事件。
執行您想要目前排程器排定的工作。
呼叫 concurrency::CurrentScheduler::Detach 方法,中斷目前排程器的連結並且還原先前的排程器做為目前的排程器。
如果您使用 Scheduler::Create 方法,請呼叫 concurrency::Scheduler::Release 方法將 Scheduler 物件的參考計數遞減。
將事件的控制代碼傳遞給 WaitForSingleObject 函式,以等候排程器關閉。
呼叫 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();
}
這個範例產生下列輸出。
編譯程式碼
請複製範例程式碼,並將它貼在 Visual Studio 專案中,或貼在名為 scheduler-instance.cpp 的檔案中,然後在 Visual Studio 的 [命令提示字元] 視窗中執行下列命令。
cl.exe /EHsc scheduler-instance.cpp