aboutsummaryrefslogtreecommitdiffstats
path: root/include/threadpool.h
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2016-03-01 08:43:56 -0500
committerClyne Sullivan <tullivan99@gmail.com>2016-03-01 08:43:56 -0500
commit47f8aa5b312a5ef671e83322bcbe201a034f84c0 (patch)
tree8dccb7228c407e01024752b48dae7150b6b349e5 /include/threadpool.h
parent32cb1880f018fc149d1c8a71a83426a8f5a92a6a (diff)
parent883b348abac73d6c2b1d4ea8b65caccf0767e5a8 (diff)
merge with remake
Diffstat (limited to 'include/threadpool.h')
-rw-r--r--include/threadpool.h53
1 files changed, 53 insertions, 0 deletions
diff --git a/include/threadpool.h b/include/threadpool.h
new file mode 100644
index 0000000..c341673
--- /dev/null
+++ b/include/threadpool.h
@@ -0,0 +1,53 @@
+#ifndef THREADPOOL_H
+#define THREADPOOL_H
+
+#include <vector>
+#include <queue>
+#include <thread>
+#include <mutex>
+#include <condition_variable>
+#include <iostream>
+#include <unistd.h>
+
+using namespace std;
+
+class ThreadPool
+{
+public:
+
+ // Constructor.
+ ThreadPool(int threads);
+
+ // Destructor.
+ ~ThreadPool();
+
+ // Adds task to a task queue.
+ void Enqueue(function<void()> f);
+
+ // Shut down the pool.
+ void ShutDown();
+
+private:
+ // Thread pool storage.
+ vector<thread> threadPool;
+
+ // Queue to keep track of incoming tasks.
+ queue<function<void()>> tasks;
+
+ // Task queue mutex.
+ mutex tasksMutex;
+
+ // Condition variable.
+ condition_variable condition;
+
+ // Indicates that pool needs to be shut down.
+ bool terminate;
+
+ // Indicates that pool has been terminated.
+ bool stopped;
+
+ // Function that will be invoked by our threads.
+ void Invoke();
+};
+
+#endif //THRE \ No newline at end of file