@@ -64,8 +64,10 @@ class Job {
6464 } catch (...) {
6565 task->error = std::current_exception ();
6666 }
67- if (--num_pending_tasks_ == 0 )
67+ if (--num_pending_tasks_ == 0 ) {
68+ std::lock_guard<std::mutex> g (mtx_);
6869 cv_.notify_one ();
70+ }
6971 };
7072 } catch (...) { // if, for whatever reason, we cannot initialize the task, we should erase it
7173 tasks_.erase (it);
@@ -113,7 +115,7 @@ class Job {
113115 }
114116
115117 private:
116- std::mutex mtx_; // this is a dummy mutex - we could just use atomic_wait on num_pending_tasks_
118+ std::mutex mtx_; // could just probably use atomic_wait on num_pending_tasks_
117119 std::condition_variable cv_;
118120 std::atomic_int num_pending_tasks_{0 };
119121 bool started_ = false ;
@@ -126,7 +128,7 @@ class Job {
126128
127129 // This needs to be a container which never invalidates references when inserting new items.
128130 std::multimap<priority_t , Task, std::greater<priority_t >,
129- mm::detail::object_pool_allocator<std::pair<priority_t , Task>>> tasks_;
131+ mm::detail::object_pool_allocator<std::pair<const priority_t , Task>>> tasks_;
130132};
131133
132134class ThreadPoolBase {
@@ -191,7 +193,10 @@ class ThreadPoolBase {
191193 assert (this_thread_pool () == this );
192194 std::unique_lock lock (mtx_);
193195 do {
194- cv_.wait (lock, [&]() { return stop_requested_ || !tasks_.empty (); });
196+ for (;;) {
197+ bool ret;
198+ while (!(ret = condition) && !stop_requested_ && tasks_.empty ())
199+ cv_.wait (lock);
195200 }
196201
197202 }
@@ -216,15 +221,15 @@ inline void ThreadPoolBase::AddTask(TaskFunc f) {
216221 {
217222 std::lock_guard<std::mutex> g (mtx_);
218223 if (stop_requested_)
219- throw std::logic_error (" The thread pool is stopped and no longer accepts new tasks." );
224+ throw std::logic_error (" The thread pool is stopped and no longer accepts new tasks." );
220225 tasks_.push (std::move (f));
221226 }
222227 cv_.notify_one ();
223228}
224229
225230inline void ThreadPoolBase::Run (int index) noexcept {
226231 ThreadPoolBase *this_thread_pool_ = this ;
227- this_thread_idx_ = index;
232+ this_thread_index_ = index;
228233 OnThreadStart (index);
229234 detail::CallAtExit ([&]() { OnThreadStop (index); });
230235 std::unique_lock lock (mtx_);
0 commit comments