C++ Library Extensions 2022.12.09
To help learn modern C++ programming
045-parallel_for.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <type_traits>
3#include <mutex>
4#include <thread>
5#include <execution>
6
7#if defined(_MSVC_LANG) || defined(_ICL)
8 #include <ppl.h>
9 namespace ccy = concurrency;
10#else
11 #include <tbb/tbb.h>
12 namespace ccy = tbb;
13#endif
14
15
16template<typename CallbackType, typename PolicyType, typename BeginType, typename EndType>
17bool parallel_for(CallbackType&& callback, PolicyType&& policy,
18 BeginType begin_index, EndType end_index)
19{
20 // std::vector<size_t> dummy_vector((size_t)1);
21
22 // auto begin = dummy_vector.begin();
23 // auto end = begin;
24 // std::advance(end, (end_index-begin_index));
25
26 // std::atomic<bool> operation_success{true};
27
28 // auto callback_wrapper = [&operation_success, &begin, &callback](auto& itr)
29 // {
30 // if(operation_success)
31 // {
32 // try
33 // {
34 // callback((BeginType)std::distance(begin, &itr));
35 // }
36 // catch(...)
37 // {
38 // operation_success = false;
39 // }
40 // }
41 // };
42
43 // std::for_each(policy, begin, end, callback_wrapper);
44
45 return true; // operation_success;
46}
47
49{
51 using lock_type = std::lock_guard<std::mutex>;
52
53 auto callback = [&mutex](auto index)
54 {
55 lock_type lock(mutex);
56
57 std::cout << "Thread ID = " <<std::this_thread::get_id() <<
58 "\tIndex = " << index << std::endl;
59 };
60
61 ccy::parallel_for(1, 11, callback);
62}
63
64int main()
65{
67}
68
std::mutex mutex
Definition: 022-mutex.cpp:12
auto & cout
bool parallel_for(CallbackType &&callback, PolicyType &&policy, BeginType begin_index, EndType end_index)
void test_parallel_for()
int main()
auto & endl