Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor/Optional update to c++17 #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# TinyThreadPool
C++11 tiny thread pool
C++17 tiny thread pool
83 changes: 38 additions & 45 deletions TinyThreadPool.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
#ifndef THREAD_POOL_H
#define THREAD_POOL_H

#include<vector>
#include<queue>
#include<memory>
#include<future>
#include<mutex>
#include<future>
class TinyThreadPool {
// Changes in 2023: Changed 'result_of' to 'invoke_result_t' as it is not a member of 'std' in C++17

#include <vector>
#include <queue>
#include <memory>
#include <future>
#include <mutex>
#include <condition_variable>

class TinyThreadPool {
public:
TinyThreadPool(size_t);
template<class F, class... Args>
auto enqueue(F&& f, Args&& ... args)
->std::future<typename std::result_of<F(Args...)>::type>;
~TinyThreadPool();
template <class F, class... Args>
auto enqueue(F&& f, Args&&... args) -> std::future<std::invoke_result_t<F, Args...>>;
~TinyThreadPool();

private:
std::vector<std::thread> workers;
std::queue<std::function<void()>> tasks;
Expand All @@ -24,61 +26,52 @@ class TinyThreadPool {
};

inline TinyThreadPool::TinyThreadPool(size_t threads) : stop(false) {

for(size_t i = 0; i < threads; i++) {

workers.emplace_back(
[this]
{
for(;;) {
std::function<void()> tasks;
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->condition.wait(lock,
[this]{ return this->stop || !this->tasks.empty();});
if(this->stop && this->tasks.empty()) {
return;
}
tasks = std::move(this->tasks.front());
this->tasks.pop();
for (size_t i = 0; i < threads; i++) {
workers.emplace_back([this] {
for (;;) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->condition.wait(lock, [this] { return this->stop || !this->tasks.empty(); });
if (this->stop && this->tasks.empty()) {
return;
}
tasks();
task = std::move(this->tasks.front());
this->tasks.pop();
}
task();
}
);
}
});
}
}
template<class F, class ... Args>
auto TinyThreadPool::enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>
{
using return_type = typename std::result_of<F(Args...)>::type;

auto task = std::make_shared<std::packaged_task<return_type()> >(
std::bind(std::forward<F>(f), std::forward<Args>(args) ...)
);
template <class F, class... Args>
auto TinyThreadPool::enqueue(F&& f, Args&&... args) -> std::future<std::invoke_result_t<F, Args...>> {
using return_type = std::invoke_result_t<F, Args...>;

auto task = std::make_shared<std::packaged_task<return_type()>>(std::bind(std::forward<F>(f), std::forward<Args>(args)...));

std::future<return_type> res = task->get_future();
{
std::unique_lock<std::mutex> lock(queue_mutex);
if(stop) {
throw std::runtime_error("enqueue on stop ThreadPool");
if (stop) {
throw std::runtime_error("enqueue on stopped ThreadPool");
}
tasks.emplace([task](){(*task)();});
tasks.emplace([task] { (*task)(); });
}
condition.notify_one();
return res;
}


inline TinyThreadPool::~TinyThreadPool() {
{
std::unique_lock<std::mutex> lock(queue_mutex);
stop = true;
}
condition.notify_all();
for(std::thread &worker : workers) {
for (std::thread& worker : workers) {
worker.join();
}
}
#endif

#endif