Execution policies in C++ are primarily designed for use with standard library algorithms to enable parallel and vectorized execution.
However, you can also apply the concept of execution policies to your custom algorithms with some additional work. This involves explicitly managing how tasks are dispatched and executed.
Here’s how you can create a custom algorithm that supports execution policies:
std::execution::seq
, std::execution::par
, or std::execution::par_unseq
.<thread>
, <future>
, or parallel algorithms from the <execution>
header to manage task execution based on the provided policy.Here’s an example:
#include <algorithm>
#include <execution>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
// Mutex to make Log function atomic
std::mutex log_mutex;
template <typename Iterator, typename Function>
void custom_for_each(
std::execution::sequenced_policy,
Iterator first, Iterator last, Function fn
) {
std::for_each(first, last, fn);
}
template <typename Iterator, typename Function>
void custom_for_each(
std::execution::parallel_policy,
Iterator first, Iterator last, Function fn) {
std::vector<std::thread> threads;
for (auto it = first; it != last; ++it) {
threads.emplace_back(fn, *it);
}
for (auto& t : threads) {
t.join();
}
}
template <typename Iterator, typename Function>
void custom_for_each(
std::execution::parallel_unsequenced_policy,
Iterator first, Iterator last, Function fn
) {
std::for_each(
std::execution::par_unseq, first, last, fn);
}
void Log(int number) {
std::lock_guard<std::mutex> guard(log_mutex);
std::cout << "Number: " << number << '\n';
}
int main() {
std::vector<int> numbers{1, 2, 3, 4, 5};
custom_for_each(
std::execution::par,
numbers.begin(), numbers.end(), Log
);
}
Number: 2
Number: 1
Number: 3
Number: 4
Number: 5
In this example, custom_for_each()
supports both sequential and parallel execution policies. For parallel execution, it spawns a new thread for each element in the range.
Key points:
std::execution::seq
, std::execution::par
, std::execution::par_unseq
) can inspire the design of custom algorithms.By following these guidelines, you can extend the power of execution policies to your custom algorithms, improving their performance and scalability.
Answers to questions are automatically generated and may not have been reviewed.
Multithreading in C++ standard library algorithms using execution policies