Sdílet prostřednictvím


Postupy: Správa instance plánovače

Instance služby Plánovač umožňuje přidružit konkrétní zásady plánování různé druhy pracovního vytížení.Toto téma obsahuje dva základní příklady, které ukazují, jak vytvořit a spravovat instanci plánovače.

Příklady vytvoření plánovače, které používají výchozí zásady plánovač.Příklad vytvoří Plánovač, který používá vlastní zásadu, naleznete v Postupy: Určení specifických zásad plánovače.

Správa instance plánovače v aplikaci

  1. Vytvořit concurrency::SchedulerPolicy objekt, který obsahuje zásady hodnoty použít Plánovač.

  2. Volat concurrency::CurrentScheduler::Create metoda nebo concurrency::Scheduler::Create metoda vytvoření instance plánovač.

    Použijete-li Scheduler::Create metoda, volání concurrency::Scheduler::Attach metodu, pokud je třeba přidružit k aktuálnímu kontextu plánovač.

  3. Volat Funkce CreateEvent funkce k vytvoření popisovače na objekt události-signály, automatické obnovení.

  4. Předání popisovače objektu události, který jste právě vytvořili concurrency::CurrentScheduler::RegisterShutdownEvent metoda nebo concurrency::Scheduler::RegisterShutdownEvent metody.To registruje událost, která má být nastavena, když je zničen plánovač.

  5. Provádění úkolů, které chcete plánovat aktuální plánovač.

  6. Volat concurrency::CurrentScheduler::Detach metody odpojit aktuální Plánovač a obnovit předchozí Plánovač jako aktuální.

    Použijete-li Scheduler::Create metoda, volání concurrency::Scheduler::Release metoda sníží počet odkaz Scheduler objektu.

  7. Předání popisovače události WaitForSingleObject funkce čekání na ukončení plánovače.

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

Příklad

Následující kód ukazuje dva způsoby, jak spravovat instanci plánovače.Každý příklad používá výchozí plánovač nejprve provést úkol, který vytiskne jedinečný identifikátor pro aktuální plánovač.Každý příklad pomocí plánovače instance stejnou operaci provést znovu.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řit instance Plánovač a přidružit k aktuálnímu kontextu.V druhém příkladu concurrency::Scheduler třída provádět stejné úlohy.Obvykle CurrentScheduler třída slouží k práci s aktuální plánovač.Druhý příklad používá Scheduler třídy, je užitečné, pokud chcete řídit, když je přidružen k aktuální kontext Plánovač nebo pokud chcete přidružit jednotlivým úkolům konkrétní plánovače.

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

  

Probíhá kompilace kódu

Zkopírovat ukázkový kód a vložit jej do projektu sady Visual Studio nebo vložit do souboru s názvem Plánovač instance.cpp a potom spusťte následující příkaz v okně Příkazový řádek Visual Studio.

cl.exe /EHsc scheduler-instance.cpp

Viz také

Úkoly

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

Koncepty

Instance plánovače