-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread_pool.hpp
60 lines (48 loc) · 1.13 KB
/
thread_pool.hpp
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
#pragma once
#include "base/base.hpp"
#include "base/macros.hpp"
#include <functional>
#include <memory>
namespace threads
{
class IRoutine;
class Thread;
} // namespace threads
namespace base
{
namespace thread_pool
{
namespace routine
{
typedef std::function<void(threads::IRoutine *)> TFinishRoutineFn;
class ThreadPool
{
public:
ThreadPool(size_t size, const TFinishRoutineFn & finishFn);
~ThreadPool();
// ThreadPool will not delete routine. You can delete it in finish_routine_fn if need
void PushBack(threads::IRoutine * routine);
void PushFront(threads::IRoutine * routine);
void Stop();
private:
class Impl;
Impl * m_impl;
};
} // namespace routine
namespace routine_simple
{
/// Simple threads container. Takes ownership for every added IRoutine.
class ThreadPool
{
public:
ThreadPool(size_t reserve = 0);
void Add(std::unique_ptr<threads::IRoutine> && routine);
void Join();
threads::IRoutine * GetRoutine(size_t i) const;
private:
std::vector<std::unique_ptr<threads::Thread>> m_pool;
DISALLOW_COPY(ThreadPool);
};
} // namespace routine_simple
} // namespace thread_pool
} // namespace base