Skip to content

Execution Policies

Execution Policies

Most algorithms have overloads that accept execution policies. The standard library algorithms support several execution policies, and the library provides corresponding execution policy types and objects. Users may select an execution policy statically by invoking a parallel algorithm with an execution policy object of the corresponding type.

1
2
3
4
5
6
7
find_package(Threads)
find_package(Execution)
find_library(TBB_LIBRARY NAMES tbb)
if (Execution_FOUND AND TBB_LIBRARY)
    add_executable(policies policies.cpp)
    target_link_libraries(policies PUBLIC std::execution Threads::Threads ${TBB_LIBRARIES})
endif ()

1
2
3
4
std::vector<int> x{5, 7, 6, 4, 8, 2};
std::sort(std::execution::seq, x.begin(), x.end());
std::copy(x.begin(), x.end(), std::ostream_iterator<int>{std::cout, " "});
std::cout << '\n';

1
2
3
4
std::vector<int> y{5, 7, 6, 4, 8, 2};
std::sort(std::execution::par, y.begin(), y.end());
std::copy(y.begin(), y.end(), std::ostream_iterator<int>{std::cout, " "});
std::cout << '\n';

1
2
3
4
std::vector<int> z{5, 7, 6, 4, 8, 2};
std::sort(std::execution::par_unseq, z.begin(), z.end());
std::copy(z.begin(), z.end(), std::ostream_iterator<int>{std::cout, " "});
std::cout << '\n';

1
2
3
4
std::vector<int> w{5, 7, 6, 4, 8, 2};
std::sort(std::execution::unseq, w.begin(), w.end());
std::copy(w.begin(), w.end(), std::ostream_iterator<int>{std::cout, " "});
std::cout << '\n';

Share Snippets