Sdílet prostřednictvím


Postupy: Poskytování pracovních funkcí třídám call a transformer

Toto téma ukazuje několik způsobů, jak poskytovat funkce práce concurrency::call a concurrency::transformer třídy.

První příklad ukazuje, jak předat lambda výraz call objektu.Druhý příklad ukazuje, jak předat funkci objektu call objektu.Třetí příklad ukazuje, jak svázat metodu třídy call objektu.

Pro ilustraci, každý příklad v tomto tématu používá call třídy.Příklad používající třídu transformer najdete v části Postupy: Použití transformace v datovém kanálu.

Příklad

Následující příklad ukazuje běžný způsob použití call třídy.V tomto příkladu předává lambda funkce call konstruktoru.

// 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;
}

Tento příklad vytvoří následující výstup.

  

Následující příklad je podobný předchozímu, s tím rozdílem, že používá call třída funkce objektu (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;
}

Následující příklad je podobný předchozímu, s tím rozdílem, že používá std::bind1st a std::mem_fun funkce pro vytvoření vazby call objekt pro metodu třídy.

Tento postup použijte, pokud potřebujete svázat call nebo transformer objekt určité třídy metody namísto operátoru volání funkce 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;
}

Můžete také přiřadit výsledek bind1st funkce std::function a používat auto klíčové slovo, jak je znázorněno v následujícím příkladu.

// 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);

Probíhá kompilace kódu

Zkopírovat ukázkový kód a vložit jej do projektu sady Visual Studio nebo vložit do souboru s názvem call.cpp a potom spusťte následující příkaz v okně Příkazový řádek Visual Studio.

cl.exe /EHsc call.cpp

Viz také

Úkoly

Postupy: Použití transformace v datovém kanálu

Referenční dokumentace

call – třída

transformer – třída

Koncepty

Knihovna asynchronních agentů

Asynchronní bloky zpráv