Monday, October 30, 2017

C++17: Parallel versions of STL algorithms

C++17 added parallel versions of some STL algorithms. Here is an example:

// The following program neither compiles on MSVS 2017 nor gcc version 6.3.0.
#include <algorithm>
//#include <execution>
#include <iostream>
#include <vector>

int main()
{
  std::vector<int> myVector{3, 1, 4, 5, 9, 2, 6};
//std::sort(std::execution::par, myVector.begin(), myVector.end());
  for (auto i : myVector)
  {
    std::cout << i << " ";
  }
  std::cout << std::endl;
  return 0;
}
// Theoretical Output: 1 2 3 4 5 6 9
References:
https://en.wikipedia.org/wiki/C%2B%2B17
http://www.bfilipek.com/2017/01/cpp17features.html#merged-the-parallelism-ts-aka-parallel-stl
http://en.cppreference.com/w/cpp/algorithm/execution_policy_tag_t
http://en.cppreference.com/w/cpp/algorithm/sort

No comments:

Post a Comment