I'm trying to create an application that will perpetually monitor a folder, and add any new files to a queue for multi-threaded processing.
This is what I have:
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();
}
The only thing I need to ensure for thread safety is that I can not have one file being operated on by two threads of the same function.
The way I see it, I have two options, neither which I know how to do:
- Wait until the map is finished, check folder again for files, repopulate QStringList, and reinitiate map.
- Add to the QStringList feeding the map, assuming that the map never closes, and that you can just keep adding items to the queue and it will operate the function on them.
Are either of these possible, or is there another way I should be going about this?