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

fix msvc warning and make ThreadPoolOptions easy to use #8

Closed
wants to merge 3 commits into from
Closed
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
20 changes: 10 additions & 10 deletions thread_pool/thread_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
* @brief The ThreadPoolOptions struct provides construction options for ThreadPool.
*/
struct ThreadPoolOptions {
enum {AUTODETECT = 0};
size_t threads_count = AUTODETECT;
size_t worker_queue_size = 1024;
ThreadPoolOptions() = default;
ThreadPoolOptions(size_t queue_size, size_t count = 0) : threads_count(count), worker_queue_size(queue_size) {}
enum { AUTODETECT = 0 };
size_t threads_count = AUTODETECT;
size_t worker_queue_size = 1024;
};

/**
Expand Down Expand Up @@ -46,7 +48,7 @@ class ThreadPool {
* execution or exception thrown.
*/
template <typename Handler>
void post(Handler &&handler);
bool post(Handler &&handler);

/**
* @brief process Post piece of job to thread pool and get future for this job.
Expand Down Expand Up @@ -104,11 +106,9 @@ inline ThreadPool::~ThreadPool()
}

template <typename Handler>
inline void ThreadPool::post(Handler &&handler)
inline bool ThreadPool::post(Handler &&handler)
{
if (!getWorker().post(std::forward<Handler>(handler))) {
throw std::overflow_error("worker queue is full");
}
return getWorker().post(std::forward<Handler>(handler));
}

template <typename Handler, typename R>
Expand All @@ -120,8 +120,8 @@ typename std::future<R> ThreadPool::process(Handler &&handler)

std::future<R> result = task.get_future();

if (!getWorker().post(task)) {
throw std::overflow_error("worker queue is full");
if (!post(task)) {
return nullptr;
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion thread_pool/worker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Worker {
namespace detail {
inline size_t * thread_id()
{
static thread_local size_t tss_id = -1u;
static thread_local size_t tss_id = std::numeric_limits<int>::max();
return &tss_id;
}
}
Expand Down