次の方法で共有


方法: メッセージ ブロック フィルターを使用する

ここでは、フィルター関数を使用して、非同期メッセージ ブロックでメッセージの受け入れや拒否がメッセージ ペイロードに基づいて行われるようにする方法について説明します。

作成する場合は、メッセージ ブロック オブジェクトなど、 concurrency::unbounded_bufferconcurrency::call、または concurrency::transformer、指定できます、 フィルター関数メッセージ ブロックを受け入れるか、メッセージを拒否するかを決定します。フィルター関数は、メッセージ ブロックが特定の値のみを受信するようにする便利な方法です。

フィルター関数は、メッセージ ブロックを接続してデータ フロー ネットワークを形成できるという点で重要です。データ フロー ネットワークでは、メッセージ ブロックが特定の基準を満たすメッセージのみを処理することによってデータのフローを制御します。このモデルと制御フロー モデルを比較してください。制御フロー モデルでは、条件付きステートメントやループなどの制御構造体を使用してデータのフローが調整されます。

ここでは、メッセージ フィルターの基本的な使用例を示します。メッセージ フィルターとデータ フロー モデルを使用してメッセージ ブロックを接続する方法の別の例については、「チュートリアル: データフロー エージェントの作成」および「チュートリアル: イメージ処理ネットワークの作成」を参照してください。

使用例

次の関数 count_primes について考えます。この関数では、受信メッセージをフィルター処理しないメッセージ ブロックの基本的な使用方法を示します。メッセージ ブロックは、素数を std::vector オブジェクトに追加します。count_primes 関数は、いくつかの数をメッセージ ブロックに送信し、メッセージ ブロックから出力値を受信し、素数をコンソールに出力します。

// Illustrates usage of a message buffer that does not use filtering.
void count_primes(unsigned long random_seed)
{
    // Holds prime numbers.
    vector<unsigned long> primes;

    // Adds numbers that are prime to the vector object.
    transformer<unsigned long, unsigned long> t([&primes](unsigned long n) -> unsigned long
    {
        if (is_prime(n))
        {
            primes.push_back(n);
        }
        return n;
    });

    // Send random values to the message buffer.
    mt19937 generator(random_seed);
    for (int i = 0; i < 20; ++i)
    {
        send(t, static_cast<unsigned long>(generator()%10000));
    }

    // Receive from the message buffer the same number of times
    // to ensure that the message buffer has processed each message.
    for (int i = 0; i < 20; ++i)
    {
        receive(t);
    }

    // Print the prime numbers to the console.
    wcout << L"The following numbers are prime: " << endl;
    for(unsigned long prime : primes)
    {
        wcout << prime << endl;
    }
}

transformer オブジェクトはすべての入力値を処理します。ただし、必要なのは素数のみです。メッセージ送信側が素数のみを送信するようにアプリケーションを記述することもできますが、メッセージ受信側の要件が必ずしも判明しているとは限りません。

次の関数 count_primes_filter は、count_primes 関数と同じタスクを実行します。ただし、このバージョンの transformer オブジェクトは、フィルター関数を使用して素数のみを受け入れます。アクションを実行する関数は素数のみを受信するため、is_prime 関数を呼び出す必要はありません。

transformer オブジェクトは素数のみを受信するため、transformer オブジェクト自体にそれらの素数を保持できます。つまり、この例の transformer オブジェクトは、素数を vector オブジェクトに追加する必要はありません。

// Illustrates usage of a message buffer that uses filtering.
void count_primes_filter(unsigned long random_seed)
{
    // Accepts numbers that are prime.
    transformer<unsigned long, unsigned long> t([](unsigned long n) -> unsigned long
    {
        // The filter function guarantees that the input value is prime.
        // Return the input value.
        return n;
    },
    nullptr,
    [](unsigned long n) -> bool
    {
        // Filter only values that are prime.
        return is_prime(n);
    });

    // Send random values to the message buffer.
    mt19937 generator(random_seed);
    size_t prime_count = 0;
    for (int i = 0; i < 20; ++i)
    {
        if (send(t, static_cast<unsigned long>(generator()%10000)))
        {
            ++prime_count;
        }
    }

    // Print the prime numbers to the console. 
    wcout << L"The following numbers are prime: " << endl;
    while (prime_count-- > 0)
    {
        wcout << receive(t) << endl;
    }
}

今回の transformer オブジェクトは素数のみを処理します。前の例の transformer オブジェクトはすべてのメッセージを処理します。したがって、前の例では、送信メッセージの数と受信メッセージの数が一致する必要があります。この例の結果を使用して、 concurrency::send から受信するメッセージの数を決定する関数、 transformerオブジェクト。send 関数は、メッセージ バッファーがメッセージを受け入れた場合は true を返し、メッセージ バッファーがメッセージを拒否した場合は false を返します。したがって、メッセージ バッファーがメッセージを受け入れる回数は、素数の数と一致します。

コード例全体を次に示します。この例では、count_primes 関数と count_primes_filter 関数の両方を呼び出します。

// primes-filter.cpp
// compile with: /EHsc
#include <agents.h>
#include <algorithm>
#include <iostream>
#include <random>

using namespace concurrency;
using namespace std;

// Determines whether the input value is prime.
bool is_prime(unsigned long n)
{
    if (n < 2)
        return false;
    for (unsigned long i = 2; i < n; ++i)
    {
        if ((n % i) == 0)
            return false;
    }
    return true;
}

// Illustrates usage of a message buffer that does not use filtering.
void count_primes(unsigned long random_seed)
{
    // Holds prime numbers.
    vector<unsigned long> primes;

    // Adds numbers that are prime to the vector object.
    transformer<unsigned long, unsigned long> t([&primes](unsigned long n) -> unsigned long
    {
        if (is_prime(n))
        {
            primes.push_back(n);
        }
        return n;
    });

    // Send random values to the message buffer.
    mt19937 generator(random_seed);
    for (int i = 0; i < 20; ++i)
    {
        send(t, static_cast<unsigned long>(generator()%10000));
    }

    // Receive from the message buffer the same number of times
    // to ensure that the message buffer has processed each message.
    for (int i = 0; i < 20; ++i)
    {
        receive(t);
    }

    // Print the prime numbers to the console.
    wcout << L"The following numbers are prime: " << endl;
    for(unsigned long prime : primes)
    {
        wcout << prime << endl;
    }
}

// Illustrates usage of a message buffer that uses filtering.
void count_primes_filter(unsigned long random_seed)
{
    // Accepts numbers that are prime.
    transformer<unsigned long, unsigned long> t([](unsigned long n) -> unsigned long
    {
        // The filter function guarantees that the input value is prime.
        // Return the input value.
        return n;
    },
    nullptr,
    [](unsigned long n) -> bool
    {
        // Filter only values that are prime.
        return is_prime(n);
    });

    // Send random values to the message buffer.
    mt19937 generator(random_seed);
    size_t prime_count = 0;
    for (int i = 0; i < 20; ++i)
    {
        if (send(t, static_cast<unsigned long>(generator()%10000)))
        {
            ++prime_count;
        }
    }

    // Print the prime numbers to the console. 
    wcout << L"The following numbers are prime: " << endl;
    while (prime_count-- > 0)
    {
        wcout << receive(t) << endl;
    }
}

int wmain()
{
    const unsigned long random_seed = 99714;

    wcout << L"Without filtering:" << endl;
    count_primes(random_seed);

    wcout << L"With filtering:" << endl;
    count_primes_filter(random_seed);

    /* Output:
    9973
    9349
    9241
    8893
    1297
    7127
    8647
    3229
    With filtering:
    The following numbers are prime:
    9973
    9349
    9241
    8893
    1297
    7127
    8647
    3229
    */
}

この例を実行すると、次の出力が生成されます。

Without filtering:
The following numbers are prime:
9973
9349
9241
8893
1297
7127
8647
3229
With filtering:
The following numbers are prime:
9973
9349
9241
8893
1297
7127
8647
3229

コードのコンパイル

コード例をコピーして、Visual Studio プロジェクトでは、貼り付けるまたはという名前のファイルに貼り付けてプライム filter.cpp と、Visual Studio のコマンド プロンプト ウィンドウで次のコマンドを実行します。

cl.exe /EHsc primes-filter.cpp

信頼性の高いプログラミング

ラムダ関数、関数ポインター、または関数オブジェクトをフィルター関数として使用できます。各フィルター関数の形式は次のいずれかになります。

bool (_Type)
bool (_Type const &)

データの不必要なコピーをなくすには、値で伝達される集約型を扱うときに 2 番目の形式を使用します。

参照

処理手順

チュートリアル: データフロー エージェントの作成

チュートリアル: イメージ処理ネットワークの作成

関連項目

transformer クラス

概念

非同期エージェント ライブラリ