Skip to content

Commit

Permalink
Replace QMutex and condition QWaitCondition with std::
Browse files Browse the repository at this point in the history
  • Loading branch information
cmorty committed Mar 19, 2020
1 parent 110cb59 commit e8f0640
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
17 changes: 8 additions & 9 deletions src/dotrunner.cpp
Expand Up @@ -256,27 +256,26 @@ bool DotRunner::run()

void DotRunnerQueue::enqueue(DotRunner *runner)
{
QMutexLocker locker(&m_mutex);
std::lock_guard<std::mutex> locker(m_mutex);
m_queue.push(runner);
m_bufferNotEmpty.wakeAll();
m_bufferNotEmpty.notify_all();
}

DotRunner *DotRunnerQueue::dequeue()
{
QMutexLocker locker(&m_mutex);
while (m_queue.empty())
{
// wait until something is added to the queue
m_bufferNotEmpty.wait(&m_mutex);
}
std::unique_lock<std::mutex> locker(m_mutex);

// wait until something is added to the queue
m_bufferNotEmpty.wait(locker, [this]() {return !m_queue.empty(); });

DotRunner *result = m_queue.front();
m_queue.pop();
return result;
}

uint DotRunnerQueue::count() const
{
QMutexLocker locker(&m_mutex);
std::lock_guard<std::mutex> locker(m_mutex);
return m_queue.size();
}

Expand Down
7 changes: 3 additions & 4 deletions src/dotrunner.h
Expand Up @@ -18,10 +18,9 @@

#include "qcstring.h"
#include "qlist.h"
#include "qwaitcondition.h"
#include "qthread.h"
#include <queue>
#include "qmutex.h"
#include <mutex>

/** Minimal constant string class that is thread safe, once initialized. */
class DotConstString
Expand Down Expand Up @@ -114,9 +113,9 @@ class DotRunnerQueue
DotRunner *dequeue();
uint count() const;
private:
QWaitCondition m_bufferNotEmpty;
std::condition_variable m_bufferNotEmpty;
std::queue<DotRunner *> m_queue;
mutable QMutex m_mutex;
mutable std::mutex m_mutex;
};

/** Worker thread to execute a dot run */
Expand Down

0 comments on commit e8f0640

Please sign in to comment.