-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement constructor, run, post, and destructor.
- Loading branch information
1 parent
c7ef4c0
commit 253a068
Showing
2 changed files
with
54 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,72 @@ | ||
#include "../include/whirlpool.hpp" | ||
#include <future> | ||
#include <functional> | ||
#include <unistd.h> | ||
|
||
ThreadPool::ThreadPool(int num_threads) { | ||
active = true; | ||
for (int i = 0; i < num_threads; i++) { | ||
std::thread t(&ThreadPool::run, this); | ||
thread_pool.push_back(t); | ||
thread_pool.emplace_back(&ThreadPool::run, this); | ||
} | ||
} | ||
|
||
ThreadPool::~ThreadPool() { | ||
{ | ||
std::unique_lock<std::mutex> lock(pool_lock); | ||
active = false; | ||
cv.notify_all(); | ||
for (int i = 0; i < thread_pool.size(); i++) { | ||
thread_pool[i].join(); | ||
} | ||
|
||
for (auto& thread : thread_pool) { | ||
thread.join(); | ||
} | ||
} | ||
|
||
void ThreadPool::post(std::packaged_task<void()> task) { | ||
std::unique_lock lock(pool_lock); | ||
job_queue.emplace(task); | ||
cv.notify_one(); | ||
template <typename Function, typename... Args> | ||
auto ThreadPool::post(Function&& f, Args&&... args) -> std::future<decltype(f(args...))> { | ||
using ReturnType = decltype(f(args...)); | ||
|
||
// Create packaged task using function | ||
auto func_ptr = std::make_shared<std::packaged_task<decltype(f(args...))()>>( | ||
std::bind(std::forward<Function>(f), std::forward<Args>(args)...)); | ||
auto func_wrapper = [func_ptr]() { | ||
(*func_ptr)(); | ||
}; | ||
|
||
{ | ||
std::unique_lock<std::mutex> lock(pool_lock); | ||
job_queue.push(func_wrapper); | ||
cv.notify_one(); | ||
} | ||
|
||
// Get task future | ||
auto future = func_ptr->get_future(); | ||
return future; | ||
} | ||
|
||
void ThreadPool::run() { | ||
while(active) { | ||
std::unique_lock lock(pool_lock); | ||
cv.wait(lock, [&] { return !job_queue.empty() || !active; }); | ||
if(!active) break; | ||
// Get next job from queue | ||
std::packaged_task<void()> job; | ||
job.swap(job_queue.front()); | ||
job_queue.pop(); | ||
// Get job if thread pool is still active | ||
if(active) { | ||
auto job = std::move(job_queue.front()); | ||
job_queue.pop(); | ||
lock.unlock(); | ||
job(); | ||
} | ||
else { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
void ThreadPool::stop() { | ||
while(!job_queue.empty()) { } | ||
active = false; | ||
cv.notify_all(); | ||
} | ||
|
||
bool ThreadPool::busy() { | ||
return active; | ||
} |