blob: c34167398c0436a7cb1cc514d25d80e260e40f0d (
plain)
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
|
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include <vector>
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <iostream>
#include <unistd.h>
using namespace std;
class ThreadPool
{
public:
// Constructor.
ThreadPool(int threads);
// Destructor.
~ThreadPool();
// Adds task to a task queue.
void Enqueue(function<void()> f);
// Shut down the pool.
void ShutDown();
private:
// Thread pool storage.
vector<thread> threadPool;
// Queue to keep track of incoming tasks.
queue<function<void()>> tasks;
// Task queue mutex.
mutex tasksMutex;
// Condition variable.
condition_variable condition;
// Indicates that pool needs to be shut down.
bool terminate;
// Indicates that pool has been terminated.
bool stopped;
// Function that will be invoked by our threads.
void Invoke();
};
#endif //THRE
|