Delen via


Instructies: Werkfuncties verstrekken aan de aanroep- en transformatorklassen

Dit onderwerp illustreert verschillende manieren om werkfuncties te bieden aan de concurrency::call en concurrency::transformer classes.

In het eerste voorbeeld ziet u hoe u een lambda-expressie doorgeeft aan een call object. In het tweede voorbeeld ziet u hoe u een functieobject doorgeeft aan een call object. In het derde voorbeeld ziet u hoe u een klassemethode koppelt aan een call object.

Ter illustratie gebruikt elk voorbeeld in dit onderwerp de call klasse. Voor een voorbeeld waarbij de klasse transformer wordt gebruikt, zie Transformer gebruiken in een gegevenspijplijn.

Voorbeeld: oproepklasse

In het volgende voorbeeld ziet u een algemene manier om de call klasse te gebruiken. In dit voorbeeld wordt een lambda-functie doorgegeven aan de call constructor.

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

In dit voorbeeld wordt de volgende uitvoer geproduceerd.

13 squared is 169.

Voorbeeld: klasse aanroepen met functieobject

Het volgende voorbeeld lijkt op de vorige, behalve dat de call klasse samen met een functieobject (functor) wordt gebruikt.

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

Voorbeeld: Functies om aanroepobject te binden

Het volgende voorbeeld lijkt op de vorige, behalve dat de functies std::bind1st en std::mem_fun worden gebruikt om een call object te binden aan een klassemethode.

Gebruik deze techniek als u een call of transformer object moet binden aan een specifieke klassemethode in plaats van de operator voor functieoproep. 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;
}

U kunt ook het resultaat van de bind1st functie toewijzen aan een std::function-object of het auto trefwoord gebruiken, zoals wordt weergegeven in het volgende voorbeeld.

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

De code compileren

Kopieer de voorbeeldcode en plak deze in een Visual Studio-project, of plak deze in een bestand met de naam call.cpp en voer vervolgens de volgende opdracht uit in een Visual Studio-opdrachtpromptvenster.

cl.exe /EHsc-call.cpp

Zie ook

Bibliotheek met asynchrone agents
Asynchrone berichtblokken
Handleiding: Een transformer gebruiken in een gegevenspijplijn
gespreksklasse
transformatorklasse