Cara: Menyediakan Fungsi Kerja ke Kelas panggilan dan transformator

Topik ini menggambarkan beberapa cara untuk menyediakan fungsi kerja ke kelas konkurensi::call dan concurrency::transformer .

Contoh pertama menunjukkan cara meneruskan ekspresi lambda ke call objek. Contoh kedua menunjukkan cara meneruskan objek fungsi ke call objek. Contoh ketiga menunjukkan cara mengikat metode kelas ke call objek.

Untuk ilustrasi, setiap contoh dalam topik ini menggunakan call kelas . Untuk contoh yang menggunakan transformer kelas , lihat Cara: Menggunakan transformator dalam Alur Data.

Contoh: kelas panggilan

Contoh berikut menunjukkan cara umum untuk menggunakan call kelas . Contoh ini meneruskan fungsi lambda ke call konstruktor.

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

Contoh ini menghasilkan output berikut.

13 squared is 169.

Contoh: memanggil kelas dengan objek fungsi

Contoh berikut menyerupai yang sebelumnya, kecuali menggunakan call kelas bersama dengan objek fungsi (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;
}

Contoh: Fungsi untuk mengikat objek panggilan

Contoh berikut menyerupai yang sebelumnya, kecuali menggunakan fungsi std::bind1st dan std::mem_fun untuk mengikat call objek ke metode kelas.

Gunakan teknik ini jika Anda harus mengikat call objek atau transformer ke metode kelas tertentu alih-alih operator panggilan fungsi, 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;
}

Anda juga dapat menetapkan hasil bind1st fungsi ke objek std::function atau menggunakan auto kata kunci, seperti yang ditunjukkan dalam contoh berikut.

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

Mengompilasi Kode

Salin kode contoh dan tempelkan dalam proyek Visual Studio, atau tempelkan dalam file yang diberi nama call.cpp lalu jalankan perintah berikut di jendela Prompt Perintah Visual Studio.

cl.exe /EHsc call.cpp

Baca juga

Pustaka Agen Asinkron
Blok Pesan Asinkron
Cara: Menggunakan transformator dalam Alur Data
Kelas panggilan
Kelas transformator