-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPool.h
More file actions
34 lines (28 loc) · 725 Bytes
/
Copy pathThreadPool.h
File metadata and controls
34 lines (28 loc) · 725 Bytes
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
#pragma once
#include <functional>
#include <vector>
#include <queue>
#include <memory>
#include <future>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <iostream>
class ThreadPool {
public :
ThreadPool(size_t num_threads);
static thread_local ThreadPool *localThreadPoolPtr;
void execute(std::function<void()> const &task) {
std::unique_lock<std::mutex> lock(mutex);
queue.push(task);
conditionVariable.notify_one();
}
~ThreadPool();
private:
std::queue<std::function<void()> > queue;
std::vector<std::thread> threads;
std::atomic<bool> working;
std::mutex mutex;
std::condition_variable conditionVariable;
};