Procedura: gestire un'istanza dell'utilità di pianificazione

Le istanze dell'utilità di pianificazione consentono di associare criteri di pianificazione specifici a diversi tipi di carichi di lavoro.In questo argomento vengono forniti due esempi di base che illustrano come creare e gestire un'istanza dell'utilità di pianificazione.

Negli esempi vengono create utilità di pianificazione che utilizzano i criteri dell'utilità di pianificazione predefiniti.Per un esempio in cui viene creata un'utilità di pianificazione che utilizza criteri personalizzati, vedere Procedura: specificare criteri dell'utilità di pianificazione specifici.

Per gestire un'istanza dell'utilità di pianificazione nell'applicazione

  1. Creare un concurrency::SchedulerPolicy i valori di oggetto che contiene il criterio per utilizzare l'utilità di pianificazione.

  2. Chiamare il concurrency::CurrentScheduler::Create metodo o la concurrency::Scheduler::Create metodo per creare un'istanza dell'utilità di pianificazione.

    Se si utilizza il Scheduler::Create metodo, la chiamata di concurrency::Scheduler::Attach metodo quando è necessario associare l'utilità di pianificazione con il contesto corrente.

  3. Chiamare la funzione CreateEvent per creare un handle per un oggetto evento di reimpostazione automatica non segnalato.

  4. Passare l'handle per l'oggetto evento che avete appena creato per il concurrency::CurrentScheduler::RegisterShutdownEvent metodo o la concurrency::Scheduler::RegisterShutdownEvent metodo.In questo modo viene registrato l'evento da impostare quando viene eliminata l'utilità di pianificazione.

  5. Eseguire le attività che si desidera vengano pianificate dall'utilità di pianificazione corrente.

  6. Chiamare il concurrency::CurrentScheduler::Detach metodo per scollegare l'utilità di pianificazione corrente e ripristina l'utilità di pianificazione precedente quello corrente.

    Se si utilizza il Scheduler::Create metodo, la chiamata di concurrency::Scheduler::Release metodo per diminuire il conteggio dei riferimenti del Scheduler oggetto.

  7. Passare l'handle per l'evento alla funzione WaitForSingleObject per attendere la chiusura dell'utilità di pianificazione.

  8. Chiamare la funzione CloseHandle per chiudere l'handle per l'oggetto evento.

Esempio

Nel codice seguente vengono illustrati due modi per gestire un'istanza dell'utilità di pianificazione.In ogni esempio viene innanzitutto utilizzata l'utilità di pianificazione predefinita per eseguire un'attività che stampa l'identificatore univoco dell'utilità di pianificazione corrente.Viene quindi utilizzata un'istanza dell'utilità di pianificazione per eseguire di nuovo la stessa attività.Infine, viene ripristinata l'utilità di pianificazione predefinita come utilità corrente e viene eseguita nuovamente l'attività.

Nel primo esempio viene utilizzata la concurrency::CurrentScheduler per creare un'istanza dell'utilità di pianificazione e associarlo al contesto corrente.Nel secondo esempio viene utilizzata la concurrency::Scheduler classe per eseguire la stessa operazione.In genere, la classe CurrentScheduler viene utilizzata per gestire l'utilità di pianificazione corrente.Il secondo esempio, in cui viene utilizzata la classe Scheduler, è utile se si desidera controllare quando l'utilità di pianificazione viene associata al contesto corrente o se si desidera associare utilità di pianificazione specifiche a specifiche attività.

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

Questo esempio produce l'output che segue.

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

Compilazione del codice

Copiare il codice di esempio e incollarlo in un progetto di Visual Studio o incollarlo in un file denominato di pianificazione-instance.cpp e quindi eseguire il comando riportato di seguito in una finestra del prompt dei comandi di Visual Studio.

cl.exe /EHsc scheduler-instance.cpp

Vedere anche

Attività

Procedura: specificare criteri dell'utilità di pianificazione specifici

Concetti

Istanze dell'utilità di pianificazione