C++ 如何添加到由QtConcurrent::map操作的QStringList?
我正在尝试创建一个应用程序,该应用程序将永久监视文件夹,并将任何新文件添加到队列中进行多线程处理 这就是我所拥有的:C++ 如何添加到由QtConcurrent::map操作的QStringList?,c++,multithreading,qt,qthread,qtconcurrent,C++,Multithreading,Qt,Qthread,Qtconcurrent,我正在尝试创建一个应用程序,该应用程序将永久监视文件夹,并将任何新文件添加到队列中进行多线程处理 这就是我所拥有的: int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QDir dir("/home/boat/Programming/"); QStringList folders = QStringList() << "/home/boat/Programming/";
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QDir dir("/home/boat/Programming/");
QStringList folders = QStringList() << "/home/boat/Programming/";
QStringList filters = QStringList() << "*.boat";
QStringList boatFileNames = dir.entryList(filters);
/* Start Threading | Files from list are deleted on completion */
QtConcurrent::map(boatFileNames, &someFunction);
/* Monitor folder for new files to add to queue */
QFileSystemWatcher fsw(folders);
QObject::connect(&fsw,&QFileSystemWatcher::directoryChanged,[&](){
????
});
a.exec();
}
intmain(intargc,char*argv[])
{
qcorea应用程序(argc、argv);
QDir dir(“/home/boat/Programming/”);
QStringList文件夹=QStringList()
如何添加到正在由操作的QStringList
QtConcurrent::map?我正在尝试创建一个应用程序,它将永久监视文件夹,并将任何新文件添加到队列中以进行多线程处理
我甚至不会使用QtConcurrent::map
,因为任务可以通过受互斥体保护的简单堆栈或队列状容器来实现
将所有工作线程(使用者线程)连接到自己的信号,比如voidMyClass::newFileAvailable()
使void QFileSystemWatcher::directoryChanged(const QString&path)
发出信号以填充类似队列(或您可以使其类似堆栈)的结构:
class FilePathQueue
{
void push(const QString& path)
{
QMutexLocker lock(&mutex);
list.push_back(path);
}
QString pop()
{
QMutexLocker lock(&mutex);
if (list.isEmpty())
return QString();
QString ret = list.front();
list.pop_front();
return ret;
}
private:
QMutex mutex;
QStringList list;
};
因此,所有工作线程都应该订阅newFileAvailable()
信号,并将通过FilePathQueue::pop()
使用新的文件路径,同时检查是否还有可用的项目
/* File path thread-safe container */
FilePathQueue fileList;
/* Monitor folder for new files to add to queue */
QFileSystemWatcher fsw(folders);
QObject::connect(&fsw, &QFileSystemWatcher::directoryChanged,
[&](const QString& path){
fileList.push(path); // push in new path
emit newFileAvailable(); // let workers know
// TODO: here we can have condition.notify_all() or notify_one()
// instead of signal emit for the higher performance
});
// launch new workers
for(int n=0; n<8; ++n)
{
Worker* worker = new Worker(this, fileList); // create worker and
worker->start(); // starts own thread etc.
connect(this, &MyClass::newFileAvailable, // connect it to new
worker, &MyWorker::processNewFile); // file signal
}
使用Qt可以很简单地做到这一点,并且非常易于管理,比如说,每几个工作线程每秒有数千个文件到达信号,但是为了获得更高的速率和更多的实时性能,应该在工作线程端利用system::wait(),而发布者将执行QWaitCondition::notify_one(
)或notify_all()
取决于逻辑。要实现该逻辑,需要提供更详细的工作线程示例
// the worker thread supposed to be running in own thread
// see QObject::moveToThread()
class MyWorker : public QObject
{
public:
MyWorker(QObject* parent, FilePathQueue& list) :
QObject(parent), fileList(list) {}
///
public slots:
void processNewFile()
{
QString path = fileList.pop();
if (path.isEmpty()) return;
process(path);
}
void process(const QString& path);
private:
FilePathQueue& fileList;
};