Skip to content

Commit

Permalink
handle return value of QtConcurrent::run
Browse files Browse the repository at this point in the history
  • Loading branch information
vigsterkr authored and Viktor Gal committed Oct 12, 2022
1 parent 4dd0890 commit 47ada1f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/library/browse/foldertreemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ FolderTreeModel::FolderTreeModel(QObject* parent)
&FolderTreeModel::onHasSubDirectory);

m_pool.setMaxThreadCount(4);
QtConcurrent::run(&m_pool, [&]() {
m_folderProcess = QtConcurrent::run(&m_pool, [&]() {
while (m_isRunning.load(std::memory_order_consume)) {
if (!m_folderQueue.isEmpty()) {
m_queueLock.lock();
Expand Down Expand Up @@ -93,6 +93,7 @@ FolderTreeModel::FolderTreeModel(QObject* parent)

FolderTreeModel::~FolderTreeModel() {
m_isRunning.store(false, std::memory_order_release);
m_folderProcess.waitForFinished();
}

/* A tree model of the filesystem should be initialized lazy.
Expand Down Expand Up @@ -232,8 +233,9 @@ void FolderTreeModel::addChildren(
}

void FolderTreeModel::onHasSubDirectory(const QString& path) {
QtConcurrent::run(&m_pool, [this, path]() {
auto r = QtConcurrent::run(&m_pool, [this, path]() {
MWriteLocker lock(&m_cacheLock);
directoryHasChildren(path);
});
r.waitForFinished();
}
2 changes: 2 additions & 0 deletions src/library/browse/foldertreemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <QAbstractItemModel>
#include <QFileSystemWatcher>
#include <QFuture>
#include <QHash>
#include <QList>
#include <QModelIndex>
Expand Down Expand Up @@ -68,6 +69,7 @@ class FolderTreeModel : public TreeItemModel {
mutable FolderQueue m_folderQueue;
mutable std::mutex m_queueLock;
std::atomic<bool> m_isRunning;
QFuture<void> m_folderProcess;
};
Q_DECLARE_METATYPE(FolderTreeModel::TreeItemList);
Q_DECLARE_OPAQUE_POINTER(FolderTreeModel::TreeItemList);

0 comments on commit 47ada1f

Please sign in to comment.