다음을 통해 공유


방법: 특정 스케줄러 정책을 사용하는 에이전트 만들기

에이전트는 규모가 큰 컴퓨팅 작업을 해결하기 위해 다른 구성 요소와 함께 비동기적으로 동작하는 응용 프로그램 구성 요소입니다. 일반적으로 에이전트는 수명 주기가 설정되어 있으며 상태를 유지 관리합니다.

모든 에이전트에는 고유한 응용 프로그램 요구 사항이 있을 수 있습니다. 예를 들어 입력을 가져오거나 출력을 표시하는 등의 사용자 상호 작용이 가능한 에이전트의 경우 컴퓨팅 리소스에 대한 액세스 우선 순위가 높아야 할 수 있습니다. 스케줄러 정책을 사용하면 스케줄러가 작업을 관리할 때 사용하는 전략을 제어할 수 있습니다. 이 항목에서는 특정 스케줄러 정책을 사용하는 에이전트를 만드는 방법을 보여 줍니다.

비동기 메시지 블록과 함께 사용자 지정 스케줄러 정책을 사용하는 기본 예제를 보려면 방법: 특정 스케줄러 정책 지정을 참조하십시오.

이 항목에서는 에이전트, 메시지 블록 및 메시지 전달 함수와 같은 비동기 에이전트 라이브러리의 기능을 사용하여 작업을 수행합니다. 비동기 에이전트 라이브러리에 대한 자세한 내용은 비동기 에이전트 라이브러리를 참조하십시오.

예제

다음 예제에서는 concurrency::agent에서 파생되는 두 클래스, 즉 permutorprinter를 정의합니다. permutor 클래스는 지정된 입력 문자열의 모든 순열을 계산합니다. printer 클래스는 진행 메시지를 콘솔에 출력합니다. permutor 클래스는 계산을 많이 하는 작업을 수행하므로 사용 가능한 컴퓨팅 리소스를 모두 사용할 수도 있습니다. 따라서 printer 클래스는 적시에 각 진행 메시지를 출력해야 합니다.

이 예제에서는 printer 클래스가 컴퓨팅 리소스에 공평하게 액세스할 수 있도록 하기 위해 방법: 스케줄러 인스턴스 관리에 설명된 단계를 사용하여 사용자 지정 정책을 포함하는 스케줄러 인스턴스를 만듭니다. 사용자 지정 정책은 스레드 우선 순위가 최고 우선 순위 클래스가 되도록 지정합니다.

사용자 지정 정책을 포함하는 스케줄러를 사용할 경우의 이점을 나타내기 위해 이 예제에서는 전체 작업을 두 번 수행합니다. 이 예제에서는 먼저 기본 스케줄러를 사용하여 두 작업을 예약합니다. 그런 다음 기본 스케줄러를 사용하여 permutor 개체를 예약하고, 사용자 지정 정책을 포함하는 스케줄러를 사용하여 printer 개체를 예약합니다.

// permute-strings.cpp 
// compile with: /EHsc
#include <windows.h>
#include <ppl.h>
#include <agents.h>
#include <iostream>
#include <sstream>

using namespace concurrency;
using namespace std;

// Computes all permutations of a given input string. 
class permutor : public agent
{
public:
   explicit permutor(ISource<wstring>& source,
      ITarget<unsigned int>& progress)
      : _source(source)
      , _progress(progress)
   {
   }

   explicit permutor(ISource<wstring>& source,
      ITarget<unsigned int>& progress,
      Scheduler& scheduler)
      : agent(scheduler)
      , _source(source)
      , _progress(progress)
   {
   }

   explicit permutor(ISource<wstring>& source,
      ITarget<unsigned int>& progress,
      ScheduleGroup& group)
      : agent(group)       
      , _source(source)
      , _progress(progress)
   {
   }

protected:
   // Performs the work of the agent. 
   void run()
   {
      // Read the source string from the buffer.
      wstring s = receive(_source);

      // Compute all permutations.
      permute(s);

      // Set the status of the agent to agent_done.
      done();
   }

   // Computes the factorial of the given value. 
   unsigned int factorial(unsigned int n)
   {
      if (n == 0)
         return 0;
      if (n == 1)
         return 1;
      return n * factorial(n - 1);
   }

   // Computes the nth permutation of the given wstring.
   wstring permutation(int n, const wstring& s) 
   {
      wstring t(s);

      size_t len = t.length();
      for (unsigned int i = 2; i < len; ++i)
      {
         swap(t[n % i], t[i]);
         n = n / i;
      }
      return t;
   }

   // Computes all permutations of the given string. 
   void permute(const wstring& s)
   {      
      // The factorial gives us the number of permutations. 
      unsigned int permutation_count = factorial(s.length());

      // The number of computed permutations.
      LONG count = 0L;      

      // Tracks the previous percentage so that we only send the percentage 
      // when it changes. 
      unsigned int previous_percent = 0u;

      // Send initial progress message.
      send(_progress, previous_percent);

      // Compute all permutations in parallel.
      parallel_for (0u, permutation_count, [&](unsigned int i) {
         // Compute the permutation.
         permutation(i, s);

         // Send the updated status to the progress reader. 
         unsigned int percent = 100 * InterlockedIncrement(&count) / permutation_count;
         if (percent > previous_percent)
         {
             send(_progress, percent);
             previous_percent = percent;
         }
      });

      // Send final progress message.
      send(_progress, 100u);
   }

private:
   // The buffer that contains the source string to permute.
   ISource<wstring>& _source;

   // The buffer to write progress status to.
   ITarget<unsigned int>& _progress;
};

// Prints progress messages to the console. 
class printer : public agent
{
public:
   explicit printer(ISource<wstring>& source,
      ISource<unsigned int>& progress)
      : _source(source)
      , _progress(progress)
   {
   }

   explicit printer(ISource<wstring>& source,
      ISource<unsigned int>& progress, Scheduler& scheduler)
      : agent(scheduler)
      , _source(source)
      , _progress(progress)
   {
   }

   explicit printer(ISource<wstring>& source,
      ISource<unsigned int>& progress, ScheduleGroup& group)
      : agent(group)       
      , _source(source)
      , _progress(progress)
   {
   }

protected:
   // Performs the work of the agent. 
   void run()
   {
      // Read the source string from the buffer and print a message.
      wstringstream ss;
      ss << L"Computing all permutations of '" << receive(_source) << L"'..." << endl;
      wcout << ss.str();

      // Print each progress message. 
      unsigned int previous_progress = 0u;
      while (true)
      {         
         unsigned int progress = receive(_progress);

         if (progress > previous_progress || progress == 0u)
         { 
            wstringstream ss;
            ss << L'\r' << progress << L"% complete...";
            wcout << ss.str();
            previous_progress = progress;
         }

         if (progress == 100)
            break;
      }
      wcout << endl;

      // Set the status of the agent to agent_done.
      done();
   }

private:
   // The buffer that contains the source string to permute.
   ISource<wstring>& _source;

   // The buffer that contains progress status.
   ISource<unsigned int>& _progress;
};

// Computes all permutations of the given string.  
void permute_string(const wstring& source,
   Scheduler& permutor_scheduler, Scheduler& printer_scheduler)
{  
   // Message buffer that contains the source string. 
   // The permutor and printer agents both read from this buffer.
   single_assignment<wstring> source_string;

   // Message buffer that contains the progress status. 
   // The permutor agent writes to this buffer and the printer agent reads 
   // from this buffer.
   unbounded_buffer<unsigned int> progress;

   // Create the agents with the appropriate schedulers.
   permutor agent1(source_string, progress, permutor_scheduler);
   printer agent2(source_string, progress, printer_scheduler);

   // Start the agents.
   agent1.start();
   agent2.start();

   // Write the source string to the message buffer. This will unblock the agents.
   send(source_string, source);

   // Wait for both agents to finish.
   agent::wait(&agent1);
   agent::wait(&agent2);
}

int wmain()
{
   const wstring source(L"Grapefruit");

   // Compute all permutations on the default scheduler.

   Scheduler* default_scheduler = CurrentScheduler::Get();

   wcout << L"With default scheduler: " << endl;
   permute_string(source, *default_scheduler, *default_scheduler);
   wcout << endl;

   // Compute all permutations again. This time, provide a scheduler that 
   // has higher context priority to the printer agent.

   SchedulerPolicy printer_policy(1, ContextPriority, THREAD_PRIORITY_HIGHEST);
   Scheduler* printer_scheduler = Scheduler::Create(printer_policy);

   // Register to be notified when the scheduler shuts down.
   HANDLE hShutdownEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
   printer_scheduler->RegisterShutdownEvent(hShutdownEvent);

   wcout << L"With higher context priority: " << endl;
   permute_string(source, *default_scheduler, *printer_scheduler);
   wcout << endl; 

   // Release the printer scheduler.
   printer_scheduler->Release();

   // Wait for the scheduler to shut down and destroy itself.
   WaitForSingleObject(hShutdownEvent, INFINITE);

   // Close the event handle.
   CloseHandle(hShutdownEvent);
}

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

  

두 작업 모두 결과는 같지만 사용자 지정 정책을 사용하는 버전의 경우 printer 개체를 높은 우선 순위에서 실행할 수 있으므로 개체의 동작 반응성이 향상됩니다.

코드 컴파일

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

cl.exe /EHsc permute-strings.cpp

참고 항목

참조

How-to: Specify Specific Scheduler Policies

개념

스케줄러 정책

비동기 에이전트