forked from progschj/ThreadPool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
102 lines (91 loc) · 2.95 KB
/
example.cpp
File metadata and controls
102 lines (91 loc) · 2.95 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <vector>
#include <chrono>
#include <assert.h>
#include "ThreadPool.h"
int f() {return -1;}
class TestThreadPool
{
public:
int f0() {return 0;}
int f1(int) {return 1;}
int f2(int, int) {return 2;}
int f3(int, int, int) {return 3;}
int f4(int, int, int, int) {return 4;}
int f5(int, int, int, int, int) {return 5;}
int f6(int, int, int, int, int, int) {return 6;}
int f7(int, int, int, int, int, int, int) {return 7;}
int f8(int, int, int, int, int, int, int, int) {return 8;}
int f9(int, int, int, int, int, int, int, int, int) {return 9;}
void throw_exception()
{
throw std::runtime_error("error");
}
};
// test with lambda functions
void testLambda()
{
ThreadPool pool(4);
std::vector< std::future<int> > results;
for(int i = 0; i < 8; ++i) {
results.push_back(
pool.enqueue([i] {
std::cout << "hello " << i << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "world " << i << std::endl;
return i*i;
})
);
}
for(size_t i = 0;i<results.size();++i)
std::cout << results[i].get() << ' ';
std::cout << std::endl;
}
// test with class member functions and static functions
void testMemberFunction()
{
//Test usage with member functions
ThreadPool pool(4);
TestThreadPool w;
std::future<int> result = pool.enqueue(f);
std::future<int> result0 = pool.enqueue(&TestThreadPool::f0, &w);
std::future<int> result1 = pool.enqueue(&TestThreadPool::f1, &w, 1);
std::future<int> result2 = pool.enqueue(&TestThreadPool::f2, &w, 1,2);
std::future<int> result3 = pool.enqueue(&TestThreadPool::f3, &w, 1,2,3);
std::future<int> result4 = pool.enqueue(&TestThreadPool::f4, &w, 1,2,3,4);
#ifndef _MSC_VER
std::future<int> result5 = pool.enqueue(&TestThreadPool::f5, &w, 1,2,3,4,5);
std::future<int> result6 = pool.enqueue(&TestThreadPool::f6, &w, 1,2,3,4,5,6);
std::future<int> result7 = pool.enqueue(&TestThreadPool::f7, &w, 1,2,3,4,5,6,7);
std::future<int> result8 = pool.enqueue(&TestThreadPool::f8, &w, 1,2,3,4,5,6,7,8);
std::future<int> result9 = pool.enqueue(&TestThreadPool::f9, &w, 1,2,3,4,5,6,7,8,9);
#endif
std::future<void> result_except = pool.enqueue(&TestThreadPool::throw_exception, &w);
assert(result.get() == -1);
assert(result0.get() == 0);
assert(result1.get() == 1);
assert(result2.get() == 2);
assert(result3.get() == 3);
assert(result4.get() == 4);
#ifndef _MSC_VER
assert(result5.get() == 5);
assert(result6.get() == 6);
assert(result7.get() == 7);
assert(result8.get() == 8);
assert(result9.get() == 9);
#endif
try
{
result_except.get();
}
catch(std::runtime_error& e)
{
std::cerr << "caught exception: " << e.what() << std::endl;
}
}
int main()
{
testLambda();
testMemberFunction();
return 0;
}