Postupy: Správa instance plánovače

Instance Plánovače umožňují přidružit konkrétní zásady plánování k různým druhům úloh. Toto téma obsahuje dva základní příklady, které ukazují, jak vytvořit a spravovat instanci plánovače.

Příklady vytvářejí plánovače, které používají výchozí zásady plánovače. Příklad, který vytvoří plánovač, který používá vlastní zásady, naleznete v tématu Postupy: Určení konkrétních zásad plánovače.

Správa instance plánovače v aplikaci

  1. Vytvořte objekt concurrency::SchedulerPolicy , který obsahuje hodnoty zásad, které má plánovač použít.

  2. Volání concurrency::CurrentScheduler::Create metoda nebo concurrency::Scheduler::Create metoda pro vytvoření instance plánovače.

    Pokud použijete metodu Scheduler::Create , zavolejte metodu concurrency::Scheduler::Attach , pokud potřebujete přidružit plánovač k aktuálnímu kontextu.

  3. Voláním funkce CreateEvent vytvoříte popisovač objektu události bez signálu a automatického resetování.

  4. Předejte popisovač objektu události, který jste právě vytvořili do concurrency::CurrentScheduler::RegisterShutdownEvent metoda nebo concurrency::Scheduler::RegisterShutdownEvent metoda. Tím se zaregistruje událost, která se nastaví při zničení plánovače.

  5. Proveďte úlohy, které má aktuální plánovač naplánovat.

  6. Volání souběžnosti::CurrentScheduler::D etach metoda odpojit aktuální plánovač a obnovit předchozí plánovač jako aktuální.

    Pokud používáte metodu Scheduler::Create , zavolejte concurrency::Scheduler::Release metoda dekrementace referenčního počtu objektu Scheduler .

  7. Předejte popisovač události funkci WaitForSingleObject , aby se plánovač vypnul.

  8. Volání CloseHandle funkce zavřít popisovač k objektu události.

Příklad

Následující kód ukazuje dva způsoby správy instance plánovače. Každý příklad nejprve používá výchozí plánovač k provedení úlohy, která vytiskne jedinečný identifikátor aktuálního plánovače. Každý příklad pak použije instanci plánovače k dalšímu provedení stejné úlohy. Každý příklad nakonec obnoví výchozí plánovač jako aktuální a provede úlohu ještě jednou.

První příklad používá concurrency::CurrentScheduler třída vytvořit instanci plánovače a přidružit ji k aktuálnímu kontextu. Druhý příklad používá třídu concurrency::Scheduler k provedení stejné úlohy. CurrentScheduler Třída se obvykle používá k práci s aktuálním plánovačem. Druhý příklad, který používá Scheduler třídu, je užitečný, když chcete řídit, kdy je plánovač přidružen k aktuálnímu kontextu nebo když chcete přidružit konkrétní plánovače k určitým úkolům.

// 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

Zkopírujte ukázkový kód a vložte ho do projektu sady Visual Studio nebo ho vložte do pojmenovaného scheduler-instance.cpp souboru a potom v okně příkazového řádku sady Visual Studio spusťte následující příkaz.

cl.exe /EHsc scheduler-instance.cpp

Viz také

Instance plánovače
Postupy: Určení specifických zásad plánovače