Thank you all for the reply.
I have another question, if I want to still use the same function, but I would like to use Mutex, like the following case
//GLOBAL definition
//GLOBAL definition
QMutex mutex;
To copy to clipboard, switch view to plain text mode
bool MainWindow::saveImages(QList<QImage> &list, int id_article)
{
mutex.lock();
if( !dir.exists(path) )
if( !dir.mkpath(path) )
{
mutex.unlock();
return false;
}
bool ret = true;
int i = 0;
for(i=0; i< list.count(); i++) //this could take a lof of time. depending on the images number
{
if( file.exists(path + fname) )
file.remove(path + fname);
ret = list[i].save(path + fname, "jpg", 100);
if(!ret)
{
qDebug() << "saveImages(): Saving image " << fname << " Unsuccessfull";
ret = false;
break;
}
}
mutex.unlock();
return ret;
}
bool MainWindow::saveImages(QList<QImage> &list, int id_article)
{
mutex.lock();
QString path = /path/to/drive
QDir dir;
if( !dir.exists(path) )
if( !dir.mkpath(path) )
{
mutex.unlock();
return false;
}
bool ret = true;
int i = 0;
for(i=0; i< list.count(); i++) //this could take a lof of time. depending on the images number
{
QString fname = QString("%1-%2.jpg").arg(id_article).arg(i+1);
if( file.exists(path + fname) )
file.remove(path + fname);
ret = list[i].save(path + fname, "jpg", 100);
if(!ret)
{
qDebug() << "saveImages(): Saving image " << fname << " Unsuccessfull";
ret = false;
break;
}
}
mutex.unlock();
return ret;
}
To copy to clipboard, switch view to plain text mode
here I removed all the static code, but I added the mutex variable (global one).
if I run this code
QFuture<bool> future_1 = QtConcurrent::run(&saveImages, list_1, id_1); //list_1 is 100's images, and it can take a few minutes
saveImagesWatcher.setFuture(future_1);
.... do some work to get list_2, and id_2
QFuture<bool> future_2 = QtConcurrent::run(&saveImages, list_2, id_2);//list_2 is a couple of images and it will take a few seconds
saveImagesWatcher.setFuture(future_2);
QFuture<bool> future_1 = QtConcurrent::run(&saveImages, list_1, id_1); //list_1 is 100's images, and it can take a few minutes
saveImagesWatcher.setFuture(future_1);
.... do some work to get list_2, and id_2
QFuture<bool> future_2 = QtConcurrent::run(&saveImages, list_2, id_2);//list_2 is a couple of images and it will take a few seconds
saveImagesWatcher.setFuture(future_2);
To copy to clipboard, switch view to plain text mode
This make the program save the images in list 1 before saving the images in list 2, right?
Is this a good way to enforce locking of the function? and ensure serialization access to the saveImages function?
Thanks a lot
Bookmarks