-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththread_pool_balanced_job.cpp
60 lines (50 loc) · 1.57 KB
/
thread_pool_balanced_job.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
51
52
53
54
55
56
57
58
59
60
//
// Created by yryang on 2021/10/20.
//
#include "thread_pool_balanced_job.h"
#include <iostream>
#include <vector>
#include <chrono>
#include "util.h"
namespace thread_pool_balanced_job{
int func(){
// std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << &std::this_thread::get_id << std::endl;
return 222;
}
struct functor{
int a;
int operator()(){
return a;
}
};
void test(){
thread_pool_balanced_job pool;
int N = 1000;
int task_num = 10;
std::vector<std::thread> threads;
std::vector<std::future<int> > futures;
for (int i = 0; i < task_num; ++i) {
std::vector<int> v;
for (int j = 0; j < N/task_num; ++j) {
v.push_back(j + (N/task_num)*i);
}
functor f{i};
futures.push_back(pool.submit(f));
}
std::this_thread::sleep_for(std::chrono::seconds(4));
for (int i = 0; i < futures.size(); ++i) {
int res = futures[i].get();
std::cout << "res " << res << std::endl;
ASSERT(res == i, "wrong");
}
std::cout << "test success\n";
}
}
// TODO, static member initialization must be contained in .cc file.
thread_local thread_pool_balanced_job::work_stealing_queue* thread_pool_balanced_job::thread_pool_balanced_job::local_work_queue = nullptr;
thread_local unsigned int thread_pool_balanced_job::thread_pool_balanced_job::my_index_ = -1;
int main(){
thread_pool_balanced_job::test();
return 0;
}