mirror of
https://github.com/ceres-solver/ceres-solver.git
synced 2026-08-29 16:40:38 +08:00
Converts std::unique_lock to std::lock_guard.
Tested by compiling for CXX threads, OpenMP, no threads, and TBB. Change-Id: If1ba5cfce83e2ad4e1015354ce67f5b23e89101f
This commit is contained in:
@@ -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<std::mutex> lock(mutex_);
|
||||
std::lock_guard<std::mutex> 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<std::mutex> lock(mutex_);
|
||||
std::lock_guard<std::mutex> 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<std::mutex> lock(mutex_);
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
wait_ = false;
|
||||
work_pending_condition_.notify_all();
|
||||
}
|
||||
|
||||
// Enable threads to block on Wait calls.
|
||||
void EnableWaiters() {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
wait_ = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -189,7 +189,7 @@ TEST(ConcurrentQueue, EnsureWaitBlocks) {
|
||||
|
||||
std::thread thread([&]() {
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
waiting = true;
|
||||
}
|
||||
|
||||
@@ -197,7 +197,7 @@ TEST(ConcurrentQueue, EnsureWaitBlocks) {
|
||||
bool valid = queue.Wait(&element);
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
std::lock_guard<std::mutex> 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<std::mutex> lock(mutex);
|
||||
std::lock_guard<std::mutex> 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<std::mutex> lock(mutex);
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
waiting = true;
|
||||
}
|
||||
|
||||
@@ -242,7 +242,7 @@ TEST(ConcurrentQueue, StopAndEnableWaiters) {
|
||||
bool valid = queue.Wait(&element);
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
waiting = false;
|
||||
value = element;
|
||||
valid_value = valid;
|
||||
@@ -256,7 +256,7 @@ TEST(ConcurrentQueue, StopAndEnableWaiters) {
|
||||
|
||||
// Ensure the thread is waiting.
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
EXPECT_TRUE(waiting);
|
||||
}
|
||||
|
||||
@@ -286,7 +286,7 @@ TEST(ConcurrentQueue, StopAndEnableWaiters) {
|
||||
|
||||
// Ensure nothing is popped off the queue.
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
EXPECT_TRUE(waiting);
|
||||
ASSERT_FALSE(valid_value);
|
||||
ASSERT_EQ(0, value);
|
||||
|
||||
@@ -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<std::mutex> lock(mutex_);
|
||||
std::lock_guard<std::mutex> 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<std::mutex> lock(shared_state->mutex_i);
|
||||
std::lock_guard<std::mutex> lock(shared_state->mutex_i);
|
||||
if (shared_state->i >= shared_state->num_work_items) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ ThreadPool::ThreadPool(int num_threads) {
|
||||
}
|
||||
|
||||
ThreadPool::~ThreadPool() {
|
||||
std::unique_lock<std::mutex> lock(thread_pool_mutex_);
|
||||
std::lock_guard<std::mutex> 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<std::mutex> lock(thread_pool_mutex_);
|
||||
std::lock_guard<std::mutex> 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<void()>& func) {
|
||||
}
|
||||
|
||||
int ThreadPool::Size() {
|
||||
std::unique_lock<std::mutex> lock(thread_pool_mutex_);
|
||||
std::lock_guard<std::mutex> lock(thread_pool_mutex_);
|
||||
return thread_pool_.size();
|
||||
}
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ TEST(ThreadPool, AddTask) {
|
||||
|
||||
for (int i = 0; i < num_tasks; ++i) {
|
||||
thread_pool.AddTask([&]() {
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
std::lock_guard<std::mutex> 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<std::mutex> lock(mutex);
|
||||
std::lock_guard<std::mutex> 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<std::mutex> lock(mutex);
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
++value;
|
||||
condition.notify_all();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user