Merge "thread_pool: fix stress test and move notify under lock"
diff --git a/src/base/threading/thread_pool.cc b/src/base/threading/thread_pool.cc
index 2097126..30843b3 100644
--- a/src/base/threading/thread_pool.cc
+++ b/src/base/threading/thread_pool.cc
@@ -39,13 +39,11 @@
}
void ThreadPool::PostTask(std::function<void()> fn) {
- {
- std::lock_guard<std::mutex> guard(mutex_);
- pending_tasks_.emplace_back(std::move(fn));
+ std::lock_guard<std::mutex> guard(mutex_);
+ pending_tasks_.emplace_back(std::move(fn));
if (thread_waiting_count_ == 0) {
return;
}
- }
thread_waiter_.notify_one();
}
diff --git a/src/base/threading/thread_pool_unittest.cc b/src/base/threading/thread_pool_unittest.cc
index f4aa0f7..dd70258 100644
--- a/src/base/threading/thread_pool_unittest.cc
+++ b/src/base/threading/thread_pool_unittest.cc
@@ -86,20 +86,21 @@
}
TEST(ThreadPoolTest, StressTest) {
- std::atomic<uint32_t> atomic(0);
+ std::mutex mu;
std::condition_variable cv;
+ uint32_t count = 0;
base::ThreadPool pool(128);
for (uint32_t i = 0; i < 1024; ++i) {
- pool.PostTask([&atomic, &cv] {
- if (atomic.fetch_add(1) == 1023) {
+ pool.PostTask([&mu, &count, &cv] {
+ std::lock_guard<std::mutex> guard(mu);
+ if (++count == 1024) {
cv.notify_one();
}
});
}
- std::mutex mu;
std::unique_lock<std::mutex> lock(mu);
- cv.wait(lock, [&atomic]() { return atomic.load() == 1024u; });
+ cv.wait(lock, [&count]() { return count == 1024u; });
}
} // namespace