-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy paththread-pool.h
51 lines (41 loc) · 1.22 KB
/
thread-pool.h
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
/**
* File: thread-pool.h
* -------------------
* This class defines the ThreadPool class, which accepts a collection
* of thunks (which are zero-argument functions that don't return a value)
* and schedules them in a FIFO manner to be executed by a constant number
* of child threads that exist solely to invoke previously scheduled thunks.
*/
#ifndef _thread_pool__
#define _thread_pool__
#include <cstdlib>
#include <functional>
class ThreadPool {
public:
/**
* Constructs a ThreadPool configured to spawn up to the specified
* number of threads.
*/
ThreadPool(size_t numThreads);
/**
* Destroys the ThreadPool class
*/
~ThreadPool();
/**
* Schedules the provided thunk (which is something that can
* be invoked as a zero-argument function without a return value)
* to be executed by one of the ThreadPool's threads as soon as
* all previously scheduled thunks have been handled.
*/
void schedule(const std::function<void(void)>& thunk);
/**
* Blocks and waits until all previously scheduled thunks
* have been executed in full.
*/
void wait();
private:
class ThreadPoolImpl *impl;
ThreadPool(const ThreadPool& original) = delete;
ThreadPool& operator=(const ThreadPool& rhs) = delete;
};
#endif