Jak: Správa Instance Plánovač
Plánovač instance umožňují přiřadit určité zásady plánování různé druhy pracovního vytížení.Toto téma obsahuje dvě základní příklady, které ukazují, jak vytvořit a spravovat Plánovač instance.
Příklady vytváří plánovače, které používají výchozí zásady plánovače.Příklad vytvářející Plánovač používá vlastní zásadu, naleznete v Jak: zadat specifické zásady Plánovač.
Správa služby Plánovač instance v aplikaci
Vytvoření concurrency::SchedulerPolicy hodnoty objektu, který obsahuje zásady použití plánovače.
Volání concurrency::CurrentScheduler::Create metoda nebo concurrency::Scheduler::Create k vytvoření instance Plánovač.
Používáte-li Scheduler::Create metodu, volání concurrency::Scheduler::Attach metodu potřebujete Plánovač přidružit k aktuálnímu kontextu.
Volání Funkce CreateEvent funkce pro vytvoření popisovače objektu události než signalizováno následným tichem, automatické obnovení.
Předat popisovač objektu události, který jste právě vytvořili concurrency::CurrentScheduler::RegisterShutdownEvent metoda nebo concurrency::Scheduler::RegisterShutdownEvent metoda.To registruje událostí nastavit při Plánovač zničen.
Úkoly, které má aktuální Plánovač naplánovat.
Volání concurrency::CurrentScheduler::Detach metody odpojit aktuální Plánovač a obnovit předchozí Plánovač jako aktuální.
Používáte-li Scheduler::Create metodu, volání concurrency::Scheduler::Release snížit počet referenční metody Scheduler objektu.
Předat popisovač události WaitForSingleObject funkci vypnout Plánovač čekat.
Volání CloseHandle funkce zavřít popisovač objektu události.
Příklad
Následující kód ukazuje dva způsoby správy Plánovač instance.Každý první příkladu výchozí Plánovač provedení úkolu, který vytiskne jedinečný identifikátor aktuální plánovače.Každý potom příkladu instance Plánovač znovu provést stejný úkol.Nakonec každý příklad obnoví výchozí Plánovač jako aktuální a provede úkol ještě jednou.
První příklad používá concurrency::CurrentScheduler třídy vytvořte instanci služby Plánovač a přidružit ho k aktuálnímu kontextu.V druhém příkladu concurrency::Scheduler třídy provést stejný úkol.Obvykle CurrentScheduler třída slouží k práci s aktuální plánovače.Druhý příklad, který používá Scheduler třídy, je užitečné, pokud chcete řídit, kdy plánovač souvisí s aktuální kontext nebo pokud chcete přidružit plánovače určité specifické úkoly.
// 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();
}
Tento příklad vytvoří následující výstup.
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
Probíhá kompilace kódu
Příklad kódu zkopírujte a vložte do projektu Visual Studio nebo vložit do souboru s názvem Plánovač instance.cpp a spusťte následující příkaz v okně příkazového řádku Visual Studio.
cl.exe /EHsc scheduler-instance.cpp
Viz také
Úkoly
Jak: zadat specifické zásady Plánovač