aboutsummaryrefslogtreecommitdiffstats
path: root/src/threadpool.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/threadpool.cpp')
-rw-r--r--src/threadpool.cpp99
1 files changed, 0 insertions, 99 deletions
diff --git a/src/threadpool.cpp b/src/threadpool.cpp
deleted file mode 100644
index c4f1c4a..0000000
--- a/src/threadpool.cpp
+++ /dev/null
@@ -1,99 +0,0 @@
-#include <threadpool.hpp>
-
-/**
- * Stolen from some guy.
- */
-
-// Constructor.
-ThreadPool::ThreadPool(int threads) :
- terminate(false),
- stopped(false)
-{
- // Create number of required threads and add them to the thread pool vector.
- for(int i = 0; i < threads; i++)
- {
- threadPool.emplace_back(thread(&ThreadPool::Invoke, this));
- }
-}
-
-void ThreadPool::Enqueue(function<void()> f)
-{
- // Scope based locking.
- {
- // Put unique lock on task mutex.
- unique_lock<mutex> lock(tasksMutex);
-
- // Push task into queue.
- tasks.push(f);
- }
-
- // Wake up one thread.
- condition.notify_one();
-}
-
-void ThreadPool::Invoke() {
-
- function<void()> task;
- while(true)
- {
- // Scope based locking.
- {
- // Put unique lock on task mutex.
- unique_lock<mutex> lock(tasksMutex);
-
- // Wait until queue is not empty or termination signal is sent.
- condition.wait(lock, [this]{ return !tasks.empty() || terminate; });
-
- // If termination signal received and queue is empty then exit else continue clearing the queue.
- if (terminate && tasks.empty())
- {
- return;
- }
-
- // Get next task in the queue.
- task = tasks.front();
-
- // Remove it from the queue.
- tasks.pop();
- }
-
- // Execute the task.
- task();
- }
-}
-
-void ThreadPool::ShutDown()
-{
- // Scope based locking.
- {
- // Put unique lock on task mutex.
- unique_lock<mutex> lock(tasksMutex);
-
- // Set termination flag to true.
- terminate = true;
- }
-
- // Wake up all threads.
- condition.notify_all();
-
- // Join all threads.
- for(thread &thread : threadPool)
- {
- thread.join();
- }
-
- // Empty workers vector.
- threadPool.empty();
-
- // Indicate that the pool has been shut down.
- stopped = true;
-}
-
-// Destructor.
-ThreadPool::~ThreadPool()
-{
- if (!stopped)
- {
- ShutDown();
- }
-}