-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfor_each.cpp
50 lines (42 loc) · 1.32 KB
/
for_each.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <stdio.h>
#include <thread>
#include <vector>
#include <list>
using namespace std;
bool exit_flag = false;
template<typename Iterator, typename Func>
void process(Iterator from, Iterator to, Func f)
{
for (auto it = from; it != to; ++it) {
*it = f(*it);
}
}
template<typename Iterator, typename Func>
void parallel_for_each(Iterator begin, Iterator end, Func f) {
int num_threads = thread::hardware_concurrency();
if (num_threads == 0) {
num_threads = 2;
}
const int nums_per_thread = (end - begin) / num_threads + (((end - begin) % num_threads == 0) ?0:1);
list<thread> threads;
for (auto i = 0; i < num_threads; ++i) {
Iterator from = begin + i * nums_per_thread;
Iterator to = (end - (begin + (i + 1) * nums_per_thread)) >= 0 ? begin + (i+1)*nums_per_thread : end;
threads.push_back(thread(bind(process, from, to, f)));
}
for_each(threads.begin(), threads.end(), mem_fn(&thread::join));
}
int main (int argc, char* argv[])
{
int num;
vector<int> vec;
while (cin >> num) {
vec.push_back(num);
}
parallel_for_each(vec.begin(), vec.end(), [](int x){ return -x; });
copy(vec.cbegin(), vec.cend(), ostream_iterator<int>(cout, " "));
return 0;
}