Fixed #198
This commit is contained in:
parent
3629f87627
commit
d58deddbcc
22
httplib.h
22
httplib.h
@ -285,7 +285,7 @@ public:
|
|||||||
#if CPPHTTPLIB_THREAD_POOL_COUNT > 0
|
#if CPPHTTPLIB_THREAD_POOL_COUNT > 0
|
||||||
class ThreadPool : public TaskQueue {
|
class ThreadPool : public TaskQueue {
|
||||||
public:
|
public:
|
||||||
ThreadPool(size_t n) : shutdown_(false), remaining_(0) {
|
ThreadPool(size_t n) : shutdown_(false) {
|
||||||
while (n) {
|
while (n) {
|
||||||
auto t = std::make_shared<std::thread>(worker(*this));
|
auto t = std::make_shared<std::thread>(worker(*this));
|
||||||
threads_.push_back(t);
|
threads_.push_back(t);
|
||||||
@ -303,25 +303,13 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
virtual void shutdown() override {
|
virtual void shutdown() override {
|
||||||
// Handle all remaining jobs...
|
|
||||||
for (;;) {
|
|
||||||
std::unique_lock<std::mutex> lock(mutex_);
|
|
||||||
if (jobs_.empty()) break;
|
|
||||||
cond_.notify_one();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop all worker threads...
|
// Stop all worker threads...
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(mutex_);
|
std::unique_lock<std::mutex> lock(mutex_);
|
||||||
shutdown_ = true;
|
shutdown_ = true;
|
||||||
remaining_ = threads_.size();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (;;) {
|
cond_.notify_all();
|
||||||
std::unique_lock<std::mutex> lock(mutex_);
|
|
||||||
if (!remaining_) break;
|
|
||||||
cond_.notify_all();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Join...
|
// Join...
|
||||||
for (auto t : threads_) {
|
for (auto t : threads_) {
|
||||||
@ -342,7 +330,7 @@ private:
|
|||||||
pool_.cond_.wait(
|
pool_.cond_.wait(
|
||||||
lock, [&] { return !pool_.jobs_.empty() || pool_.shutdown_; });
|
lock, [&] { return !pool_.jobs_.empty() || pool_.shutdown_; });
|
||||||
|
|
||||||
if (pool_.shutdown_) { break; }
|
if (pool_.shutdown_ && pool_.jobs_.empty()) { break; }
|
||||||
|
|
||||||
fn = pool_.jobs_.front();
|
fn = pool_.jobs_.front();
|
||||||
pool_.jobs_.pop_front();
|
pool_.jobs_.pop_front();
|
||||||
@ -351,9 +339,6 @@ private:
|
|||||||
assert(true == (bool)fn);
|
assert(true == (bool)fn);
|
||||||
fn();
|
fn();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_lock<std::mutex> lock(pool_.mutex_);
|
|
||||||
pool_.remaining_--;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ThreadPool &pool_;
|
ThreadPool &pool_;
|
||||||
@ -364,7 +349,6 @@ private:
|
|||||||
std::list<std::function<void()>> jobs_;
|
std::list<std::function<void()>> jobs_;
|
||||||
|
|
||||||
bool shutdown_;
|
bool shutdown_;
|
||||||
size_t remaining_;
|
|
||||||
|
|
||||||
std::condition_variable cond_;
|
std::condition_variable cond_;
|
||||||
std::mutex mutex_;
|
std::mutex mutex_;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user