Anteckning
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
Med Scheduler-instanser kan du associera specifika schemaläggningsprinciper med olika typer av arbetsbelastningar. Det här avsnittet innehåller två grundläggande exempel som visar hur du skapar och hanterar en scheduler-instans.
Exemplen skapar schemaläggare som använder standardschemaläggarprinciper. Ett exempel som skapar en schemaläggare som använder en anpassad princip finns i Så här anger du specifika scheduler-principer.
Hantera en scheduler-instans i ditt program
Skapa ett samtidighetsobjekt::SchedulerPolicy-objekt som innehåller de principvärden som schemaläggaren ska använda.
Anropa concurrency::CurrentScheduler::Create-metoden eller concurrency::Scheduler::Create-metoden för att skapa en scheduler-instans.
Om du använder
Scheduler::Createmetoden anropar du metoden concurrency::Scheduler::Attach när du behöver associera schemaläggaren med den aktuella kontexten.Anropa funktionen CreateEvent för att skapa ett handtag till ett icke-signalerat händelseobjekt för automatisk återställning.
Skicka handtaget till händelseobjektet som du just skapade till metoden concurrency::CurrentScheduler::RegisterShutdownEvent eller till metoden concurrency::Scheduler::RegisterShutdownEvent. Detta registrerar händelsen som ska sättas när schemaläggaren tas bort.
Utför de uppgifter som du vill att den aktuella schemaläggaren ska schemalägga.
Anropa concurrency::CurrentScheduler::Detach-metoden för att lossa den aktuella schemaläggaren och återställa den tidigare schemaläggaren som den nuvarande.
Om du använder
Scheduler::Createmetoden anropar du metoden concurrency::Scheduler::Release för att minska referensantalet förSchedulerobjektet.Skicka handtaget till händelsen till funktionen WaitForSingleObject för att vänta tills schemaläggaren stängs av.
Anropa funktionen CloseHandle för att stänga handtaget till händelseobjektet.
Exempel
Följande kod visar två sätt att hantera en scheduler-instans. Varje exempel använder först standardschemaläggaren för att utföra en uppgift som skriver ut den unika identifieraren för den aktuella schemaläggaren. Varje exempel använder sedan en scheduler-instans för att utföra samma uppgift igen. Slutligen återställer varje exempel standardschemaläggaren som den aktuella och utför uppgiften en gång till.
I det första exemplet används klassen concurrency::CurrentScheduler för att skapa en scheduler-instans och associera den med den aktuella kontexten. I det andra exemplet används samtidighet::Scheduler-klassen för att utföra samma uppgift. Vanligtvis används CurrentScheduler-klassen för att arbeta med den aktuella schemaläggaren. Det andra exemplet, som använder Scheduler klassen, är användbart när du vill styra när schemaläggaren är associerad med den aktuella kontexten eller när du vill associera specifika schemaläggare med specifika uppgifter.
// 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();
}
Det här exemplet genererar följande utdata.
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
Kompilera koden
Kopiera exempelkoden och klistra in den i ett Visual Studio-projekt, eller klistra in den i en fil med namnet scheduler-instance.cpp och kör sedan följande kommando i ett Visual Studio-kommandotolkfönster.
cl.exe /EHsc scheduler-instance.cpp
Se även
Schemaläggare-tillfällen
Så här anger du specifika schemaläggningsprinciper