Share via


Procedura: Fornire funzioni lavoro alle classi call e transformer

Questo argomento illustra diversi modi per fornire funzioni di lavoro alle classi concurrency::call e concurrency::transformer .

Il primo esempio illustra come passare un'espressione lambda a un call oggetto . Il secondo esempio mostra come passare un oggetto funzione a un call oggetto . Il terzo esempio mostra come associare un metodo di classe a un call oggetto .

Ad esempio, ogni esempio di questo argomento usa la call classe . Per un esempio che usa la transformer classe , vedere Procedura: Usare il trasformatore in una pipeline di dati.

Esempio: classe di chiamata

L'esempio seguente illustra un modo comune per usare la call classe . In questo esempio viene passata una funzione lambda al call costruttore .

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

Questo esempio produce il seguente output:

13 squared is 169.

Esempio: chiamare la classe con l'oggetto funzione

L'esempio seguente è simile a quello precedente, ad eccezione del fatto che usa la call classe insieme a un oggetto funzione (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;
}

Esempio: Funzioni per associare l'oggetto chiamata

L'esempio seguente è simile a quello precedente, ad eccezione del fatto che usa le funzioni std::bind1st e std::mem_fun per associare un call oggetto a un metodo di classe.

Usare questa tecnica se è necessario associare un call oggetto o transformer a un metodo di classe specifico anziché all'operatore di chiamata di funzione , 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;
}

È anche possibile assegnare il risultato della bind1st funzione a un oggetto std::function o usare la auto parola chiave , come illustrato nell'esempio seguente.

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

Compilazione del codice

Copiare il codice di esempio e incollarlo in un progetto di Visual Studio oppure incollarlo in un file denominato call.cpp e quindi eseguire il comando seguente in una finestra del prompt dei comandi di Visual Studio.

cl.exe /EHsc call.cpp

Vedi anche

Libreria di agenti asincroni
Blocchi dei messaggi asincroni
Procedura: Usare la classe transformer in una pipeline di dati
Classe call
Classe transformer