I have been trying to create a thread pool class(for personal experimentation/use/fun). I found a way to accept any function with any arguments/return type by using parameter packs (seen in code below) and binding the functions to std::function. This works without issue. The problem is that I want to attempt to make a member function to retrieve returned values from the jobs being executed. I do not know how to make the code generic while attempting to do this.
So far I have tried making a map, that uses the jobs ID as a key and will have the return value of that job stored in it. My struggle is 1. I cant figure out how to determine the type (I can with "typename std::invoke_result::type" but this breaks down if the type is void) 2. How to make a map that can have the Job ID as a key and any type so that I can put return types there.
class ThreadPool{
public:
//getInstance to allow the second constructor to be called
static ThreadPool& getInstance(int numThreads){
static ThreadPool instance(numThreads);
return instance;
}
//add any arg # function to queue
template <typename Func, typename... Args >
inline uint64_t push(Func& f, Args&&... args){
auto funcToAdd = std::bind(f, args...);
uint64_t newID = currentID++;
std::unique_lock<std::mutex> lock(JobMutex);
JobQueue.push(std::make_pair(funcToAdd, newID));
thread.notify_one();
return newID; //return the ID of the job in the queue
}
/* utility functions will go here*/
inline void resize(int newTCount){
int tmp = MAX_THREADS;
if(newTCount > tmp || newTCount < 1){
throw bad_thread_alloc("Cannot allocate " + std::to_string(newTCount) + " threads because it is greater than your systems maximum of " + std::to_string(tmp), __FILE__, __LINE__);
}
numThreads = (uint8_t)newTCount;
Pool.resize(newTCount);
DEBUG("New size is: " + std::to_string(Pool.size()));
}
inline uint8_t getThreadCount(){
return numThreads;
}
//how i want the user to interact with this class is
// int id = push(func, args);
// auto value = getReturnValue(id); //blocks until return value is returned
auto getReturnValue(uint64_t jobID) {
//Not sure how to handle this
}
private:
uint64_t currentID;
uint8_t numThreads;
std::vector<std::thread> Pool; //the actual thread pool
std::queue<std::pair<std::function<void()>, uint64_t>> JobQueue; //the jobs with their assigned ID
std::condition_variable thread;
std::mutex JobMutex;
/* infinite loop function */
void threadManager();
/* Constructors */
ThreadPool(); //prevent default constructor from being called
//real constructor that is used
inline ThreadPool(uint8_t numThreads) : numThreads(numThreads) {
currentID = 0; //initialize currentID
int tmp = MAX_THREADS;
if(numThreads > tmp){
throw bad_thread_alloc("Cannot allocate " + std::to_string(numThreads) + " threads because it is greater than your systems maximum of " + std::to_string(tmp), __FILE__, __LINE__);
}
for(int i = 0; i != numThreads; ++i){
Pool.push_back(std::thread(&ThreadPool::threadManager, this));
Pool.back().detach();
DEBUG("Thread " + std::to_string(i) + " allocated");
}
DEBUG("Number of threads being allocated " + std::to_string(numThreads));
}
/* end constructors */
NULL_COPY_AND_ASSIGN(ThreadPool);
}; /* end ThreadPool Class */
void ThreadPool::threadManager(){
while (true) {
std::unique_lock<std::mutex> lock(JobMutex);
thread.wait(lock, [this] {return !JobQueue.empty(); });
//strange bug where it will continue even if the job queue is empty
if (JobQueue.size() < 1)
continue;
auto job = JobQueue.front().first;
JobQueue.pop();
job();
}
}
Am I going about this all wrong? I didnt know any other way to generically store any type of function while also being able to get return types out of them.