Skip to content

Commit c896d4e

Browse files
committed
use predicate of conditional::wait instead of hand-crafted loop
1 parent 7d56e83 commit c896d4e

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

db-copy.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,12 @@ void db_copy_thread_t::add_buffer(std::unique_ptr<db_cmd_t> &&buffer)
2929
{
3030
assert(m_worker.joinable()); // thread must not have been finished
3131

32-
for (;;) {
33-
std::unique_lock<std::mutex> lock(m_queue_mutex);
34-
if (m_worker_queue.size() >= db_cmd_copy_t::Max_buffers) {
35-
m_queue_full_cond.wait(lock);
36-
continue;
37-
}
32+
std::unique_lock<std::mutex> lock(m_queue_mutex);
33+
m_queue_full_cond.wait(lock,
34+
[&]{ return m_worker_queue.size() < db_cmd_copy_t::Max_buffers; });
3835

39-
m_worker_queue.push_back(std::move(buffer));
40-
m_queue_cond.notify_one();
41-
break;
42-
}
36+
m_worker_queue.push_back(std::move(buffer));
37+
m_queue_cond.notify_one();
4338
}
4439

4540
void db_copy_thread_t::sync_and_wait()
@@ -69,10 +64,7 @@ void db_copy_thread_t::worker_thread()
6964
std::unique_ptr<db_cmd_t> item;
7065
{
7166
std::unique_lock<std::mutex> lock(m_queue_mutex);
72-
if (m_worker_queue.empty()) {
73-
m_queue_cond.wait(lock);
74-
continue;
75-
}
67+
m_queue_cond.wait(lock, [&]{ return !m_worker_queue.empty(); });
7668

7769
item = std::move(m_worker_queue.front());
7870
m_worker_queue.pop_front();

0 commit comments

Comments
 (0)