다음을 통해 공유


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

이 항목에서는 작업 기능을 제공 하는 여러 방법을 보여 줍니다.의 concurrency::callconcurrency::transformer 클래스입니다.

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

설명을 위해 이 항목의 모든 예제에서는 call 클래스를 사용합니다.transformer 클래스를 사용하는 예제를 보려면 방법: 데이터 파이프라인에서 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.

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

// 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::bind1ststd::mem_fun 함수를 사용하여 call 개체를 클래스 메서드에 바인딩한다는 점이 다릅니다.

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

// 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 개체에 할당하거나 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 클래스

개념

비동기 에이전트 라이브러리

비동기 메시지 블록