Aracılığıyla paylaş


Nasıl yapılır: çağrı ve transformer sınıflar çalışma işlevleri sağlayan

Bu konuda iş işlevlerini sağlamak için çeşitli yollar gösterilmiştir concurrency::call ve concurrency::transformer sınıfları.

İlk örnek lambda ifade için nasıl gösterir bir call nesnesi.İkinci örnek işlevi nesneye geçmesine gösterilmiştir bir call nesnesi.Üçüncü örnek bir sınıf yöntemi bağlanılamıyor gösterilmiştir bir call nesnesi.

Bu konudaki her örnek resimde için kullandığı call sınıfı.Kullanan bir örnek için transformer sınıfı için bkz: Nasıl yapılır: veri potansiyel kullanım transformer.

Örnek

Yaygın bir yolu kullanmak için aşağıdaki örnekte gösterildiği call sınıfı.Bu örnek lambda işlevine geçirir call kurucusu.

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

Bu örnek aşağıdaki çıktıyı üretir.

13 squared is 169.

Kullandığı dışında aşağıdaki örnek önceki bir benzer call sınıf işlevi nesnesi (functor) ile birlikte.

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

Kullandığı dışında aşağıdaki örnek önceki bir benzer std::bind1st ve std::mem_fun bağlamak için İşlevler bir call nesnesine bir sınıf yöntemi.

BIND varsa, bu tekniği kullanmak bir call veya transformer işlev çağrısı işleci yerine belirli bir sınıf yöntemi nesneyi 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;
}

Sonucu da atayabilirsiniz bind1st çalışması için bir std::function nesne veya kullanmak auto aşağıdaki örnekte gösterildiği gibi anahtar.

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

Kod Derleniyor

Örnek kodu kopyalayın ve Visual Studio Project'te yapıştırın veya adlı bir dosyaya yapıştırın call.cpp ve Visual Studio komut istemi penceresinde aşağıdaki komutu çalıştırın.

cl.exe /EHsc call.cpp

Ayrıca bkz.

Görevler

Nasıl yapılır: veri potansiyel kullanım transformer

Başvuru

Çağrı sınıfı

Transformer sınıfı

Kavramlar

Zaman uyumsuz aracıları kitaplığı

Zaman uyumsuz ileti blokları