C++ Library Extensions 2022.12.09
To help learn modern C++ programming
036-parallel_for.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <mutex>
3#include <thread>
4#include <execution> // for C++ Standard Parallel Algorithm
5
6#if defined(_MSVC_LANG)
7
8 #include <ppl.h> // for Microsoft Parallel Pattern Library
9 namespace ccy = concurrency; // ccy is short for namespace concurrency
10#else // for GNU g++ / clang++ / Intel Compilers
11
12 #include <tbb/tbb.h> // for Intel's Threading Building Blocks.
13 namespace ccy = tbb; // ccy is short name for Threading Building Blocks
14#endif
15
17{
19 using lock_type = std::lock_guard<std::mutex>;
20
21 auto callback = [&mutex](auto index)
22 {
23 lock_type lock(mutex);
24
25 std::cout << "Thread ID: " << std::this_thread::get_id()
26 << "\tIndex = " << index << std::endl;
27 };
28
29 ccy::parallel_for(1, 11, callback);
30}
31
32int main()
33{
35}
std::mutex mutex
Definition: 022-mutex.cpp:12
void test_parallel_for()
int main()
auto & cout
bool parallel_for(CallbackType &&callback, PolicyType &&policy, BeginType begin_index, EndType end_index)
auto & endl