Skip to content

Commit

Permalink
DataBuffer.h: use std::scoped_lock (C++17)
Browse files Browse the repository at this point in the history
  • Loading branch information
jj1bdx committed Sep 3, 2022
1 parent 445e4b4 commit 61f5bf7
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions include/DataBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ template <class Element> class DataBuffer {
void push(std::vector<Element> &&samples) {
if (!samples.empty()) {
{
std::lock_guard<std::mutex> lock(m_mutex);
std::scoped_lock<std::mutex> lock{m_mutex};
m_qlen += samples.size();
m_queue.push(std::move(samples));
// unlock m_mutex here by getting out of scope
Expand All @@ -46,7 +46,7 @@ template <class Element> class DataBuffer {
/** Mark the end of the data stream. */
void push_end() {
{
std::lock_guard<std::mutex> lock(m_mutex);
std::scoped_lock<std::mutex> lock{m_mutex};
m_end_marked = true;
// unlock m_mutex here by getting out of scope
}
Expand All @@ -56,7 +56,7 @@ template <class Element> class DataBuffer {
/** Return number of samples in queue. */
std::size_t queued_samples() {
{
std::lock_guard<std::mutex> lock(m_mutex);
std::scoped_lock<std::mutex> lock{m_mutex};
return (m_qlen);
// unlock m_mutex here by getting out of scope
}
Expand Down Expand Up @@ -86,16 +86,20 @@ template <class Element> class DataBuffer {
/** Return true if the end has been reached at the Pull side. */
bool pull_end_reached() {
{
std::lock_guard<std::mutex> lock(m_mutex);
std::scoped_lock<std::mutex> lock{m_mutex};
return ((m_qlen == 0) && (m_end_marked));
// unlock m_mutex here by getting out of scope
}
}

/** Wait until the buffer contains minfill samples or an end marker. */
void wait_buffer_fill(std::size_t minfill) {
std::unique_lock<std::mutex> lock(m_mutex);
m_cond.wait(lock, [&] { return !((m_qlen < minfill) && (!m_end_marked)); });
{
std::unique_lock<std::mutex> lock(m_mutex);
m_cond.wait(lock,
[&] { return !((m_qlen < minfill) && (!m_end_marked)); });
// unlock m_mutex here by getting out of scope
}
}

private:
Expand Down

0 comments on commit 61f5bf7

Please sign in to comment.