Udostępnij za pośrednictwem


Porady: zarządzanie przypadkiem planisty

Wystąpienia usługi Scheduler umożliwiają skojarzenie określonych zasad planowania z różnymi rodzajami obciążeń. Ten temat zawiera dwa podstawowe przykłady pokazujące sposób tworzenia wystąpienia harmonogramu i zarządzania nim.

W przykładach są tworzone harmonogramy korzystające z domyślnych zasad harmonogramu. Przykład tworzenia harmonogramu korzystającego z zasad niestandardowych można znaleźć w temacie How to: Specify Specific Scheduler Policies (Instrukcje: określanie określonych zasad harmonogramu).

Aby zarządzać przypadkiem planisty w aplikacji

  1. Utwórz obiekt concurrency::SchedulerPolicy zawierający wartości zasad dla harmonogramu do użycia.

  2. Wywołaj metodę concurrency::CurrentScheduler::Create lub współbieżność::Scheduler::Create , aby utworzyć wystąpienie harmonogramu.

    Jeśli używasz Scheduler::Create metody , wywołaj metodę concurrency::Scheduler::Attach , gdy musisz skojarzyć harmonogram z bieżącym kontekstem.

  3. Wywołaj funkcję CreateEvent , aby utworzyć dojście do niesygnaliowanego obiektu zdarzenia automatycznego resetowania.

  4. Przekaż dojście do obiektu zdarzenia, który właśnie został utworzony do współbieżności::CurrentScheduler::RegisterShutdownEvent , metoda lub metoda concurrency::Scheduler::RegisterShutdownEvent . Spowoduje to zarejestrowanie zdarzenia, które ma zostać ustawione podczas niszczenia harmonogramu.

  5. Wykonaj zadania, które mają być zaplanowane przez bieżący harmonogram harmonogramu.

  6. Wywołaj metodę concurrency::CurrentScheduler::D etach , aby odłączyć bieżący harmonogram i przywrócić poprzedni harmonogram jako bieżący.

    Jeśli używasz Scheduler::Create metody , wywołaj metodę concurrency::Scheduler::Release , aby usunąć liczbę Scheduler odwołań obiektu.

  7. Przekaż dojście do zdarzenia do funkcji WaitForSingleObject , aby poczekać na zamknięcie harmonogramu.

  8. Wywołaj funkcję CloseHandle , aby zamknąć uchwyt do obiektu zdarzenia.

Przykład

Poniższy kod przedstawia dwa sposoby zarządzania wystąpieniem harmonogramu. Każdy przykład najpierw używa domyślnego harmonogramu do wykonania zadania, które wyświetla unikatowy identyfikator bieżącego harmonogramu. Każdy przykład używa wystąpienia harmonogramu do ponownego wykonania tego samego zadania. Na koniec każdy przykład przywraca domyślny harmonogram jako bieżący i wykonuje zadanie jeszcze raz.

W pierwszym przykładzie użyto klasy concurrency::CurrentScheduler , aby utworzyć wystąpienie harmonogramu i skojarzyć je z bieżącym kontekstem. W drugim przykładzie użyto klasy concurrency::Scheduler do wykonania tego samego zadania. CurrentScheduler Zazwyczaj klasa jest używana do pracy z bieżącym harmonogramem. Drugi przykład, który używa Scheduler klasy, jest przydatny, gdy chcesz kontrolować, kiedy harmonogram jest skojarzony z bieżącym kontekstem lub gdy chcesz skojarzyć określone harmonogramy z określonymi zadaniami.

// 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();
}

W tym przykładzie są generowane następujące dane wyjściowe.

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

Kompilowanie kodu

Skopiuj przykładowy kod i wklej go w projekcie programu Visual Studio lub wklej go w pliku o nazwie scheduler-instance.cpp , a następnie uruchom następujące polecenie w oknie wiersza polecenia programu Visual Studio.

cl.exe /EHsc scheduler-instance.cpp

Zobacz też

Wystąpienia harmonogramu
Instrukcje: określanie specjalnych zasad harmonogramu