分享方式:


如何:為呼叫和轉換程式類別提供工作函式

本主題說明將工作函式提供給並行::call concurrency::transformer 類別的 數種方式。

第一個範例示範如何將 Lambda 運算式傳遞至 call 物件。 第二個範例示範如何將函式物件傳遞至 call 物件。 第三個範例示範如何將類別方法系結至 call 物件。

為了說明,本主題中的每個範例都會使用 類別 call 。 如需使用 類別的 transformer 範例,請參閱 如何:在資料管線 中使用轉換器。

範例:呼叫類別

下列範例示範使用 call 類別的常見方式。 此範例會將 Lambda 函式傳遞至建 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 至類別方法。

如果您必須將 或 transformer 物件系結 call 至特定類別方法,而不是函式呼叫運算子, 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

另請參閱

非同步代理程式程式庫
非同步訊息區
如何:在資料管線中使用轉換程式
call 類別
transformer 類別