Share via


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

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

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

애플리케이션에서 스케줄러 인스턴스를 관리하려면

  1. 사용할 스케줄러에 대한 정책 값을 포함하는 동시성::SchedulerPolicy 개체를 만듭니다.

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

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

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

  4. 방금 만든 이벤트 개체에 핸들을 동시성::CurrentScheduler::RegisterShutdownEvent 메서드 또는 동시성::Scheduler::RegisterShutdownEvent 메서드에 전달합니다. 이렇게 하면 스케줄러가 제거될 때 설정할 이벤트가 등록됩니다.

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

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

    이 메서드를 Scheduler::Create 사용하는 경우 동시성::Scheduler::Release 메서드를 호출하여 개체의 Scheduler 참조 수를 줄입니다.

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

  8. CloseHandle 함수를 호출하여 이벤트 개체에 대한 핸들을 닫습니다.

예시

다음 코드에서는 스케줄러 인스턴스를 관리하는 두 가지 방법을 보여 줍니다. 각 예제에서는 먼저 기본 스케줄러를 사용하여 현재 스케줄러의 고유 식별자를 출력하는 작업을 수행합니다. 그런 다음 각 예제는 스케줄러 인스턴스를 사용하여 동일한 작업을 다시 수행합니다. 마지막으로 각 예제에서는 기본 스케줄러를 현재 스케줄러로 복원하고 작업을 한 번 더 수행합니다.

첫 번째 예제에서는 동시성::CurrentScheduler 클래스를 사용하여 스케줄러 인스턴스를 만들고 현재 컨텍스트와 연결합니다. 두 번째 예제에서는 동시성::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 프로젝트에 붙여넣거나 이름이 지정된 scheduler-instance.cpp 파일에 붙여넣은 다음 Visual Studio 명령 프롬프트 창에서 다음 명령을 실행합니다.

cl.exe /EHsc scheduler-instance.cpp

참고 항목

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