concurrencyc/include/threadPool.h
Christopher Hamer 5da509e373 initial commit
2024-07-05 17:14:16 +01:00

51 lines
1.0 KiB
C++

//
// Created by Christopher Hamer on 19/06/2024.
//
#ifndef TASK1_THREADPOOL_H
#define TASK1_THREADPOOL_H
#include <condition_variable>
#include <functional>
#include <iostream>
#include <mutex>
#include <queue>
#include <thread>
using namespace std;
// Class that represents a simple thread pool
class ThreadPool {
public:
// // Constructor to creates a thread pool with given
// number of threads
ThreadPool(size_t num_threads);
// Destructor to stop the thread pool
~ThreadPool();
// Enqueue task for execution by the thread pool
void enqueue(function<void()> task);
private:
// Vector to store worker threads
vector<thread> threads_;
// Queue of tasks
queue<function<void()> > tasks_;
// Mutex to synchronize access to shared data
mutex queue_mutex_;
// Condition variable to signal changes in the state of
// the tasks queue
condition_variable cv_;
// Flag to indicate whether the thread pool should stop
// or not
bool stop_ = false;
};
#endif //TASK1_THREADPOOL_H