다음을 통해 공유


방법: 스케줄러 인스턴스 관리

스케줄러 인스턴스를 사용하면 특정한 일정 예약 정책을 다양한 유형의 업무와 연결할 수 있습니다. 이 항목에는 스케줄러 인스턴스를 만들고 관리하는 방법을 보여 주는 두 가지 기본 예제가 포함되어 있습니다.

이 예제에서는 기본 스케줄러 정책을 사용하는 스케줄러를 만듭니다. 사용자 지정 정책을 사용하는 스케줄러를 만드는 예제를 보려면 방법: 특정 스케줄러 정책 지정을 참조하십시오.

응용 프로그램에서 스케줄러 인스턴스를 관리하려면

  1. 스케줄러에서 사용할 정책 값을 포함하는 concurrency::SchedulerPolicy 개체를 만듭니다.

  2. concurrency::CurrentScheduler::Create 메서드 또는 concurrency::Scheduler::Create 메서드를 호출하여 스케줄러 인스턴스를 만듭니다.

    Scheduler::Create 메서드를 사용하는 경우에는 스케줄러를 현재 컨텍스트와 연결해야 할 때 concurrency::Scheduler::Attach 메서드를 호출합니다.

  3. CreateEvent 함수를 호출하여 신호를 받지 않는 자동 재설정 이벤트 개체에 대한 핸들을 만듭니다.

  4. 이전 단계에서 만든 이벤트 개체에 대한 핸들을 concurrency::CurrentScheduler::RegisterShutdownEvent 메서드 또는 concurrency::Scheduler::RegisterShutdownEvent 메서드에 전달합니다. 이렇게 하면 스케줄러가 소멸될 때 설정할 이벤트가 등록됩니다.

  5. 현재 스케줄러로 예약할 작업을 수행합니다.

  6. concurrency::CurrentScheduler::Detach 메서드를 호출하여 현재 스케줄러를 분리하고 이전 스케줄러를 현재 스케줄러로 복원합니다.

    Scheduler::Create 메서드를 사용하는 경우에는 concurrency::Scheduler::Release 메서드를 호출하여 Scheduler 개체의 참조 횟수를 감소시킵니다.

  7. 이벤트에 대한 핸들을 WaitForSingleObject 함수에 전달하여 스케줄러가 종료될 때까지 기다립니다.

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

이 예제의 결과는 다음과 같습니다.

  

코드 컴파일

예제 코드를 복사하여 Visual Studio 프로젝트 또는scheduler-instance.cpp 파일에 붙여넣고 Visual Studio 명령 프롬프트 창에서 다음 명령을 실행합니다.

cl.exe /EHsc scheduler-instance.cpp

참고 항목

작업

방법: 특정 스케줄러 정책 지정

개념

스케줄러 인스턴스