방법: 스케줄러 인스턴스 관리
스케줄러 인스턴스를 사용하면 특정한 일정 예약 정책을 다양한 유형의 업무와 연결할 수 있습니다.이 항목에는 스케줄러 인스턴스를 만들고 관리하는 방법을 보여 주는 두 가지 기본 예제가 포함되어 있습니다.
이 예제에서는 기본 스케줄러 정책을 사용하는 스케줄러를 만듭니다.사용자 지정 정책을 사용하는 스케줄러를 만드는 예제를 보려면 방법: 특정 스케줄러 정책 지정을 참조하십시오.
응용 프로그램에서 스케줄러 인스턴스를 관리하려면
만들기는 concurrency::SchedulerPolicy 정책을 포함 하는 개체 값 스케줄러를 사용 합니다.
호출을 concurrency::CurrentScheduler::Create 메서드 또는 concurrency::Scheduler::Create 스케줄러 인스턴스를 만드는 방법.
사용 하는 경우는 Scheduler::Create 메서드, 호출의 concurrency::Scheduler::Attach 메서드는 스케줄러는 현재 컨텍스트와 연결 하는 경우.
CreateEvent 함수를 호출하여 신호를 받지 않는 자동 재설정 이벤트 개체에 대한 핸들을 만듭니다.
방금 만든 된 이벤트 개체의 핸들을 전달의 concurrency::CurrentScheduler::RegisterShutdownEvent 메서드 또는 concurrency::Scheduler::RegisterShutdownEvent 메서드.이렇게 하면 스케줄러가 소멸될 때 설정할 이벤트가 등록됩니다.
현재 스케줄러로 예약할 작업을 수행합니다.
호출 하는 concurrency::CurrentScheduler::Detach 현재 스케줄러를 분리 하 고 현재 스케줄러 이전 복원 방법입니다.
사용 하는 경우는 Scheduler::Create 메서드, 호출의 concurrency::Scheduler::Release 의 참조 횟수를 감소 시킬 메서드는 Scheduler 개체.
이벤트에 대한 핸들을 WaitForSingleObject 함수에 전달하여 스케줄러가 종료될 때까지 기다립니다.
CloseHandle 함수를 호출하여 이벤트 개체에 대한 핸들을 닫습니다.
예제
다음 코드에서는 스케줄러 인스턴스를 관리하는 두 가지 방법을 보여 줍니다.각 예제에서는 먼저 기본 스케줄러를 사용하여 현재 스케줄러의 고유 식별자를 출력하는 작업을 수행합니다.그런 다음 스케줄러 인스턴스를 사용하여 같은 작업을 다시 수행합니다.마지막으로 기본 스케줄러를 현재 스케줄러로 복원하고 작업을 한 번 더 수행합니다.
예제는 concurrency::CurrentScheduler 클래스 스케줄러 인스턴스를 만들어 현재 컨텍스트와 연결 합니다.두 번째 예제는 concurrency::Scheduler 클래스는 동일한 작업을 수행 합니다.일반적으로 CurrentScheduler 클래스는 현재 스케줄러로 작업할 때 사용됩니다.Scheduler 클래스를 사용하는 두 번째 예제는 스케줄러가 현재 컨텍스트와 연결되는 시기를 제어하려는 경우 또는 특정 스케줄러를 특정 작업과 연결하려는 경우에 유용합니다.
// 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();
}
이 예제의 결과는 다음과 같습니다.
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
코드 컴파일
예제 코드를 복사 하 고 Visual Studio 프로젝트에 붙여 또는 라는 파일에 붙여 스케줄러-instance.cpp 및 다음 Visual Studio 명령 프롬프트 창에서 다음 명령을 실행 합니다.
cl.exe /EHsc scheduler-instance.cpp