mirror-ac/user/threadpool.h

34 lines
870 B
C
Raw Normal View History

2023-08-16 11:28:46 +02:00
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include <mutex>
#include <vector>
#include <queue>
#include <functional>
namespace global {
/*
* This ThreadPool class is a simple threadpool implementation that will allow us
* to delegate jobs to a set number of threads without the constant need to close
* and open new threads.
*/
class ThreadPool
2023-08-16 11:28:46 +02:00
{
int thread_count;
bool should_terminate;
std::mutex queue_mutex;
std::condition_variable mutex_condition;
std::vector<std::thread> threads;
std::queue<std::function<void()>> jobs;
2023-08-16 11:28:46 +02:00
void ThreadLoop();
2023-08-16 11:28:46 +02:00
public:
ThreadPool(int ThreadCount);
void QueueJob(const std::function<void()>& job);
void Stop();
bool Busy();
};
2023-08-16 11:28:46 +02:00
}
#endif