Gewusst wie: Verwenden eines Abbruchs zum Verlassen einer Parallel-Schleife

In diesem Beispiel wird gezeigt, wie mit dem Abbrechen ein paralleler Suchalgorithmus implementiert wird.

Beispiel

Das folgende Beispiel verwendet das Abbrechen, um in einem Array nach einem Element zu suchen. Die parallel_find_any-Funktion sucht mithilfe des Concurrency::parallel_for-Algorithmus und eines Concurrency::structured_task_group-Objekts nach der Position, die den angegebenen Wert enthält. Wenn eine Arbeitsfunktion den Wert ermittelt, ruft sie die Concurrency::structured_task_group::cancel-Methode auf, um die weitere Verarbeitung abzubrechen. Die Laufzeit bricht alle aktiven Aufgaben ab und startet keine neuen.

// parallel-array-search.cpp
// compile with: /EHsc
#include <ppl.h>
#include <iostream>
#include <random>

using namespace Concurrency;
using namespace std;

// Returns the position in the provided array that contains the given value, 
// or -1 if the value is not in the array.
template<typename T>
int parallel_find_any(const T a[], size_t count, const T& what)
{
   // The position of the element in the array. 
   // The default value, -1, indicates that the element is not in the array.
   int position = -1;

   // Use parallel_for to search for the element. 
   // The task group enables a work function to cancel the overall 
   // operation when it finds the result.

   structured_task_group tasks;
   tasks.run_and_wait([&]
   {
      parallel_for(std::size_t(0), count, [&](int n) {
         if (a[n] == what)
         {
            // Set the return value and cancel the remaining tasks. 
            position = n;            
            tasks.cancel();
         }
      });
   });

   return position;
}

int wmain()
{
   const size_t count = 10000;
   int values[count];

   // Fill the array with random values.
   mt19937 gen(34);
   for (size_t i = 0; i < count; ++i)
   {
      values[i] = gen()%10000;
   }

   // Search for any position in the array that contains value 3123.
   const int what = 3123;
   int position = parallel_find_any(values, count, what);
   if (position >= 0)
   {
      wcout << what << L" is at position " << position << L'.' << endl;
   }
   else
   {
      wcout << what << L" is not in the array." << endl;
   }  
}

Nachfolgend wird eine Beispielausgabe für dieses Beispiel angezeigt.

3123 is at position 4739.

Der Concurrency::parallel_for-Algorithmus handelt gleichzeitig. Daher führt er die Operationen nicht in einer vorgegebenen Reihenfolge aus. Wenn das Array mehrere Instanzen des Werts enthält, kann das Ergebnis eine beliebige seiner Positionen sein.

Kompilieren des Codes

Kopieren Sie den Beispielcode, und fügen Sie ihn in ein Visual Studio-Projekt ein, oder fügen Sie ihn in eine Datei mit dem Namen parallel-array-search.cpp ein, und führe Sie dann den folgenden Befehl in einem Visual Studio 2010-Eingabeaufforderungsfenster aus.

cl.exe /EHsc parallel-array-search.cpp

Siehe auch

Referenz

parallel_for-Funktion

structured_task_group-Klasse

Konzepte

Abbruch in der PPL

Parallele Algorithmen