From 05fc04490fdd8e17b25c73ba5070c07246d64c2b Mon Sep 17 00:00:00 2001 From: Mike Vitus Date: Tue, 10 Apr 2018 17:20:07 -0700 Subject: [PATCH] Converts std::unique_lock to std::lock_guard. Tested by compiling for CXX threads, OpenMP, no threads, and TBB. Change-Id: If1ba5cfce83e2ad4e1015354ce67f5b23e89101f --- internal/ceres/concurrent_queue.h | 8 ++++---- internal/ceres/concurrent_queue_test.cc | 14 +++++++------- internal/ceres/parallel_for_cxx.cc | 4 ++-- internal/ceres/thread_pool.cc | 6 +++--- internal/ceres/thread_pool_test.cc | 6 +++--- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/internal/ceres/concurrent_queue.h b/internal/ceres/concurrent_queue.h index c4e076fa5..52e290302 100644 --- a/internal/ceres/concurrent_queue.h +++ b/internal/ceres/concurrent_queue.h @@ -83,7 +83,7 @@ class ConcurrentQueue { // Atomically push an element onto the queue. If a thread was waiting for an // element, wake it up. void Push(const T& value) { - std::unique_lock lock(mutex_); + std::lock_guard lock(mutex_); queue_.push(value); work_pending_condition_.notify_one(); } @@ -93,7 +93,7 @@ class ConcurrentQueue { bool Pop(T* value) { CHECK(value != nullptr); - std::unique_lock lock(mutex_); + std::lock_guard lock(mutex_); return PopUnlocked(value); } @@ -114,14 +114,14 @@ class ConcurrentQueue { // exit Wait() without getting a value. All future Wait requests will return // immediately if no element is present until EnableWaiters is called. void StopWaiters() { - std::unique_lock lock(mutex_); + std::lock_guard lock(mutex_); wait_ = false; work_pending_condition_.notify_all(); } // Enable threads to block on Wait calls. void EnableWaiters() { - std::unique_lock lock(mutex_); + std::lock_guard lock(mutex_); wait_ = true; } diff --git a/internal/ceres/concurrent_queue_test.cc b/internal/ceres/concurrent_queue_test.cc index 3b15c4be5..698966ae2 100644 --- a/internal/ceres/concurrent_queue_test.cc +++ b/internal/ceres/concurrent_queue_test.cc @@ -189,7 +189,7 @@ TEST(ConcurrentQueue, EnsureWaitBlocks) { std::thread thread([&]() { { - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); waiting = true; } @@ -197,7 +197,7 @@ TEST(ConcurrentQueue, EnsureWaitBlocks) { bool valid = queue.Wait(&element); { - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); waiting = false; value = element; valid_value = valid; @@ -209,7 +209,7 @@ TEST(ConcurrentQueue, EnsureWaitBlocks) { // Ensure nothing is has been popped off the queue { - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); EXPECT_TRUE(waiting); ASSERT_FALSE(valid_value); ASSERT_EQ(0, value); @@ -234,7 +234,7 @@ TEST(ConcurrentQueue, StopAndEnableWaiters) { auto task = [&]() { { - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); waiting = true; } @@ -242,7 +242,7 @@ TEST(ConcurrentQueue, StopAndEnableWaiters) { bool valid = queue.Wait(&element); { - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); waiting = false; value = element; valid_value = valid; @@ -256,7 +256,7 @@ TEST(ConcurrentQueue, StopAndEnableWaiters) { // Ensure the thread is waiting. { - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); EXPECT_TRUE(waiting); } @@ -286,7 +286,7 @@ TEST(ConcurrentQueue, StopAndEnableWaiters) { // Ensure nothing is popped off the queue. { - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); EXPECT_TRUE(waiting); ASSERT_FALSE(valid_value); ASSERT_EQ(0, value); diff --git a/internal/ceres/parallel_for_cxx.cc b/internal/ceres/parallel_for_cxx.cc index 3da5a8767..20a689dd2 100644 --- a/internal/ceres/parallel_for_cxx.cc +++ b/internal/ceres/parallel_for_cxx.cc @@ -60,7 +60,7 @@ class BlockUntilFinished { // Increment the number of jobs that have finished and signal the blocking // thread if all jobs have finished. void Finished() { - std::unique_lock lock(mutex_); + std::lock_guard lock(mutex_); ++num_finished_; CHECK_LE(num_finished_, num_total_); if (num_finished_ == num_total_) { @@ -196,7 +196,7 @@ void ParallelFor(ContextImpl* context, { // Get the next available chunk of work to be performed. If there is no // work, return false. - std::unique_lock lock(shared_state->mutex_i); + std::lock_guard lock(shared_state->mutex_i); if (shared_state->i >= shared_state->num_work_items) { return false; } diff --git a/internal/ceres/thread_pool.cc b/internal/ceres/thread_pool.cc index 9c7bb898a..8fc7f837e 100644 --- a/internal/ceres/thread_pool.cc +++ b/internal/ceres/thread_pool.cc @@ -62,7 +62,7 @@ ThreadPool::ThreadPool(int num_threads) { } ThreadPool::~ThreadPool() { - std::unique_lock lock(thread_pool_mutex_); + std::lock_guard lock(thread_pool_mutex_); // Signal the thread workers to stop and wait for them to finish all scheduled // tasks. Stop(); @@ -72,7 +72,7 @@ ThreadPool::~ThreadPool() { } void ThreadPool::Resize(int num_threads) { - std::unique_lock lock(thread_pool_mutex_); + std::lock_guard lock(thread_pool_mutex_); const int num_current_threads = thread_pool_.size(); if (num_current_threads >= num_threads) { @@ -92,7 +92,7 @@ void ThreadPool::AddTask(const std::function& func) { } int ThreadPool::Size() { - std::unique_lock lock(thread_pool_mutex_); + std::lock_guard lock(thread_pool_mutex_); return thread_pool_.size(); } diff --git a/internal/ceres/thread_pool_test.cc b/internal/ceres/thread_pool_test.cc index 5485fe417..2b1bf872d 100644 --- a/internal/ceres/thread_pool_test.cc +++ b/internal/ceres/thread_pool_test.cc @@ -59,7 +59,7 @@ TEST(ThreadPool, AddTask) { for (int i = 0; i < num_tasks; ++i) { thread_pool.AddTask([&]() { - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); ++value; condition.notify_all(); }); @@ -96,7 +96,7 @@ TEST(ThreadPool, ResizingDuringExecution) { auto task = [&]() { // This will block until the mutex is released inside the condition // variable. - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); ++value; condition.notify_all(); }; @@ -150,7 +150,7 @@ TEST(ThreadPool, Destructor) { thread_pool.AddTask([&]() { // This will block until the mutex is released inside the condition // variable. - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); ++value; condition.notify_all(); });