Porady: zapewnianie funkcji pracy dla wywoływania oraz klasy transformatora
W tym temacie przedstawiono kilka sposobów udostępniania funkcji roboczych klas współbieżności::call i concurrency::transformer .
W pierwszym przykładzie pokazano, jak przekazać wyrażenie lambda do call
obiektu. W drugim przykładzie pokazano, jak przekazać obiekt funkcji do call
obiektu. W trzecim przykładzie pokazano, jak powiązać metodę klasy z obiektem call
.
Na potrzeby ilustracji każdy przykład w tym temacie używa call
klasy . Przykład używający transformer
klasy można znaleźć w temacie How to: Use transformer in a Data Pipeline (Jak używać funkcji przekształcania w potoku danych).
Przykład: wywołanie klasy
W poniższym przykładzie przedstawiono typowy sposób używania call
klasy . W tym przykładzie do konstruktora call
przekazuje funkcję lambda.
// 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;
}
W tym przykładzie są generowane następujące dane wyjściowe.
13 squared is 169.
Przykład: wywoływanie klasy z obiektem funkcji
Poniższy przykład przypomina poprzedni, z tą różnicą, że używa call
klasy razem z obiektem funkcji (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;
}
Przykład: funkcje do powiązania obiektu wywołania
Poniższy przykład przypomina poprzedni, z tą różnicą, że używa funkcji std::bind1st i std::mem_fun w celu powiązania call
obiektu z metodą klasy.
Użyj tej techniki, jeśli musisz powiązać call
obiekt lub transformer
z określoną metodą klasy zamiast operatora wywołania funkcji . 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;
}
Możesz również przypisać wynik bind1st
funkcji do obiektu std::function lub użyć auto
słowa kluczowego, jak pokazano w poniższym przykładzie.
// 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);
Kompilowanie kodu
Skopiuj przykładowy kod i wklej go w projekcie programu Visual Studio lub wklej go w pliku o nazwie call.cpp
, a następnie uruchom następujące polecenie w oknie wiersza polecenia programu Visual Studio.
cl.exe /EHsc call.cpp
Zobacz też
Biblioteki agentów asynchronicznych
Bloki komunikatów asynchronicznych
Instrukcje: używanie transformatora w potoku danych
call, klasa
transformer, klasa