std::execution::sequenced_policy, std::execution::parallel_policy, std::execution::parallel_unsequenced_policy, std::execution::unsequenced_policy
cppreference.com
<tbody>
</tbody>
class sequenced_policy { /* */ }; |
(1) | ( C++17) |
class parallel_policy { /* */ }; |
(2) | ( C++17) |
class parallel_unsequenced_policy { /* */ }; |
(3) | ( C++17) |
class unsequenced_policy { /* */ }; |
(4) | ( C++20) |
1) , , . , ( std::execution::seq), .
2) , , . , ( std::execution::par), , , . , , .
3) , , , (, ). , , .
4) , , , , , .
, , std::terminate, , .
:
int a[] = {0, 1};
std::vector<int> v;
std::for_each(std::execution::par, std::begin(a), std::end(a), [&](int i)
{
v.push_back(i * 2 + 1); // :
});
std::atomic<int> x {0};
int a[] = {1, 2};
std::for_each(std::execution::par, std::begin(a), std::end(a), [&](int)
{
x.fetch_add(1, std::memory_order_relaxed);
// :
while (x.load(std::memory_order_relaxed) == 1) { }
});
int x = 0;
std::mutex m;
int a[] = {1, 2};
std::for_each(std::execution::par, std::begin(a), std::end(a), [&](int)
{
std::lock_guard<std::mutex> guard(m);
++x; //
});
, , , . C++ ( ). - , , std::atomic , , ( , , , std::mutex::unlock std::mutex::lock).
int x = 0;
std::mutex m;
int a[] = {1, 2};
std::for_each(std::execution::par_unseq, std::begin(a), std::end(a), [&](int)
{
// : lock_guard m.lock()
std::lock_guard<std::mutex> guard(m);
++x;
});
(, - ), .
(C++17)(C++17)(C++17)(C++20) |
() |