방법: call 및 transformer 클래스에 작업 함수 제공

이 항목에서는 동시성::call동시성::변환기 클래스에 작업 함수를 제공하는 여러 가지 방법을 보여 줍니다.

첫 번째 예제에서는 개체에 람다 식을 call 전달하는 방법을 보여줍니다. 두 번째 예제에서는 개체에 함수 개체를 call 전달하는 방법을 보여 있습니다. 세 번째 예제에서는 개체에 클래스 메서드를 바인딩하는 call 방법을 보여줍니다.

예를 들어 이 항목의 모든 예제에서는 클래스를 call 사용합니다. 클래스를 사용하는 transformer 예제는 방법: 데이터 파이프라인에서 변환기 사용 방법을 참조하세요.

예제: 호출 클래스

다음 예제에서는 클래스를 사용하는 call 일반적인 방법을 보여줍니다. 이 예제에서는 생성자에 람다 함수를 call 전달합니다.

// call-lambda.cpp
// compile with: /EHsc
#include <agents.h>
#include <iostream>

using namespace concurrency;
using namespace std;

int wmain()
{
   // Stores the result of the computation.
   single_assignment<int> result;

   // Pass a lambda function to a call object that computes the square
   // of its input and then sends the result to the message buffer.
   call<int> c([&](int n) {
      send(result, n * n);
   });

   // Send a message to the call object and print the result.
   send(c, 13);
   wcout << L"13 squared is " << receive(result) << L'.' << endl;
}

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

13 squared is 169.

예: 함수 개체를 사용하여 클래스 호출

다음 예제는 함수 개체(functor)와 함께 클래스를 call 사용한다는 점을 제외하고 이전 예제와 유사합니다.

// call-functor.cpp
// compile with: /EHsc
#include <agents.h>
#include <iostream>

using namespace concurrency;
using namespace std;

// Functor class that computes the square of its input.
class square
{
public:
   explicit square(ITarget<int>& target)
      : _target(target)
   {
   }

   // Function call operator for the functor class.
   void operator()(int n)
   {
      send(_target, n * n);
   }

private:
   ITarget<int>& _target;
};

int wmain()
{
   // Stores the result of the computation.
   single_assignment<int> result;

   // Pass a function object to the call constructor.
   square s(result);
   call<int> c(s);

   // Send a message to the call object and print the result.
   send(c, 13);
   wcout << L"13 squared is " << receive(result) << L'.' << endl;
}

예: 호출 개체를 바인딩하는 함수

다음 예제는 std::bind1st 및 std::mem_fun 함수를 사용하여 개체를 클래스 메서드에 바인딩 call 한다는 점을 제외하고 이전 예제와 유사합니다.

함수 호출 연산operator()call 대신 특정 클래스 메서드에 개체를 transformer 바인딩해야 하는 경우 이 기술을 사용합니다.

// call-method.cpp
// compile with: /EHsc
#include <agents.h>
#include <functional>
#include <iostream>

using namespace concurrency;
using namespace std;

// Class that computes the square of its input.
class square
{
public:
   explicit square(ITarget<int>& target)
      : _target(target)
   {
   }

   // Method that computes the square of its input.
   void square_value(int n)
   {
      send(_target, n * n);
   }

private:
   ITarget<int>& _target;
};

int wmain()
{
   // Stores the result of the computation.
   single_assignment<int> result;

   // Bind a class method to a call object.
   square s(result);
   call<int> c(bind1st(mem_fun(&square::square_value), &s));

   // Send a message to the call object and print the result.
   send(c, 13);
   wcout << L"13 squared is " << receive(result) << L'.' << endl;
}

다음 예제와 같이 함수 결과를 bind1st std::function 개체에 할당하거나 키워드(keyword) 사용할 auto 수도 있습니다.

// Assign to a function object.
function<void(int)> f1 = bind1st(mem_fun(&square::square_value), &s);
call<int> c1(f1);

// Alternatively, use the auto keyword to have the compiler deduce the type.
auto f2 = bind1st(mem_fun(&square::square_value), &s);
call<int> c2(f2);

코드 컴파일

예제 코드를 복사하여 Visual Studio 프로젝트에 붙여넣거나 이름이 지정된 call.cpp 파일에 붙여넣은 다음 Visual Studio 명령 프롬프트 창에서 다음 명령을 실행합니다.

cl.exe /EHsc call.cpp

참고 항목

비동기 에이전트 라이브러리
비동기 메시지 블록
방법: 데이터 파이프라인에서 transformer 사용
call 클래스
transformer 클래스