/
example.cpp
81 lines (69 loc) · 2.2 KB
/
example.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<chrono>
#include<iostream>
#include<mutex>
#include<random>
#include<queue>
#include<thread>
#include"../header/CThreadPool.hpp"
namespace
{
std::mutex mut;
size_t get()
{
using namespace std;
static mt19937 mu{static_cast<mt19937::result_type>(chrono::high_resolution_clock::now().time_since_epoch().count())};
return mu()%4;
}
std::size_t add_func(const std::size_t i)
{
using namespace std;
const auto sec{get()};
this_thread::sleep_for(chrono::seconds{sec});
lock_guard<mutex> lock{mut};
cout<<"add - thread "<<i<<" wait "<<sec<<" sec"<<endl;
return i;
}
std::size_t add_and_detach_func(const std::size_t i)
{
using namespace std;
const auto sec{get()};
this_thread::sleep_for(chrono::seconds{sec});
lock_guard<mutex> lock{mut};
cout<<"add_and_detach - thread "<<i<<" wait "<<sec<<" sec"<<endl;
return i;
}
}
int main()
{
using namespace std;
nThread::CThreadPool tp{4};
queue<nThread::CThreadPool::thread_id> que;
cout<<"stage 1"<<endl;
for(size_t i{0};i!=tp.size();++i)
tp.add_and_detach(add_and_detach_func,size_t(i));
tp.join_all(); //this will not block, because you use add_and_detach
cout<<"stage 2"<<endl;
for(size_t i{0};i!=tp.size();++i)
que.push(tp.add(add_func,size_t(i))); //tp will block here until add_and_detach_func complete
for(size_t i{0};i!=tp.size();++i)
{
tp.join(que.front()); //tp will block here until the i of thread complete
que.pop();
}
cout<<"stage 3"<<endl;
for(size_t i{0};i!=tp.size();++i)
tp.add_and_detach(add_and_detach_func,size_t(i));
tp.wait_until_all_usable(); //this will block until all detach threads complete add_and_detach_func
cout<<"stage 4"<<endl;
for(size_t i{0};i!=tp.size();++i)
tp.add(add_func,size_t(i)); //tp will not block here, because you join all thread
tp.join_all(); //tp will block here until add_func complete, it is same as
//for(size_t i(0);i!=tp.size();++i)
cout<<"stage 5"<<endl;
for(size_t i{0};i!=tp.size();++i)
tp.add(add_func,size_t(i));
//you don't need to call join_all to guarantee all threads are joining
//the destructor of CThreadPool will deal with this
//something else you have to notice
//CThreadPool::join_all will not block CThreadPool::add, and vice versa
}