Anteckning
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
Det här avsnittet visar flera sätt att tillhandahålla arbetsfunktioner för concurrency::call och concurrency::transformer-klasserna.
Det första exemplet visar hur du skickar ett lambda-uttryck till ett call objekt. Det andra exemplet visar hur du skickar ett funktionsobjekt till ett call objekt. Det tredje exemplet visar hur du binder en klassmetod till ett call objekt.
För att illustrera använder varje exempel i det här avsnittet klassen call. Ett exempel som använder transformer klassen finns i Så här använder du transformering i en datapipeline.
Exempel: Call-klass
I följande exempel visas ett vanligt sätt att använda call klassen. Det här exemplet skickar en lambda-funktion till call konstruktorn.
// 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;
}
Det här exemplet genererar följande utdata.
13 squared is 169.
Exempel: anropa klass med funktionsobjekt
I följande exempel liknar det föregående, förutom att klassen används call tillsammans med ett funktionsobjekt (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;
}
Exempel: Funktioner för att binda anropsobjekt
Följande exempel liknar det föregående, förutom att det använder funktionerna std::bind1st och std::mem_fun för att binda ett call objekt till en klassmetod.
Använd den här tekniken om du måste binda ett call eller transformer objekt till en specifik klassmetod istället för funktionsanropsoperatorn, 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;
}
Du kan också tilldela resultatet av bind1st funktionen till ett std::function-objekt eller använda nyckelordet auto , som du ser i följande exempel.
// 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);
Kompilera koden
Kopiera exempelkoden och klistra in den i ett Visual Studio-projekt, eller klistra in den i en fil med namnet call.cpp och kör sedan följande kommando i ett Visual Studio-kommandotolkfönster.
cl.exe /EHsc call.cpp
Se även
Asynkront agentbibliotek
Asynkrona meddelandeblock
Anvisningar: Använda transformator i en datapipeline
anropa klass
transformeringsklass