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
16
void
test_parallel_for
()
17
{
18
std::mutex
mutex
;
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
32
int
main
()
33
{
34
test_parallel_for
();
35
}
mutex
std::mutex mutex
Definition:
022-mutex.cpp:12
test_parallel_for
void test_parallel_for()
Definition:
036-parallel_for.cpp:16
main
int main()
Definition:
036-parallel_for.cpp:32
cout
auto & cout
Definition:
044-functional.cpp:3
parallel_for
bool parallel_for(CallbackType &&callback, PolicyType &&policy, BeginType begin_index, EndType end_index)
Definition:
045-parallel_for.cpp:17
endl
auto & endl
Definition:
apply_operations.cpp:32
tbb
Definition:
cpg_iterator.hpp:19
CppExtension
tutorial
036-parallel_for.cpp
Generated by
1.9.4