C++ (Qt)#include <iostream>#include <future>#include <functional> struct worker { int get(int a) { return a*a*a*a*a*a*a; }}; int main(){ worker w; auto res = std::async(std::launch::async, std::bind(std::function<int (worker &, int)>(&worker::get), w, 2)); std::cout << res.get() << std::endl; return 0;}
for(int i=0; i<100; ++i) auto res = std::async(std::launch::async, std::bind(std::function<int (worker &, int)>(&worker::get), w, 2));
Storage storage;QImage *img[100];int error[100]; // опционально, код ошибки операции, если операция выполняется указатель пустойfor (int i=0; i<100; ++i) storage.load(QString("%1.jpg").arg(i), &img[i], *error[i]);...while(1) for(int i=0; i<100; ++i) if(img[i] && !err[i]) painter.draw(0,0,img[i]);...Чтение картинки в отдельном потоке это обычныйQImage *tmp = new QImage(fileName);img = tmp;
#include "cachedisk.h"#include <QTabWidget>CacheDisk::CacheDisk(){// connect(this, SIGNAL(Finish()), this, SLOT(Finished()), Qt::QueuedConnection); worked = false;}void CacheDisk::Read(QString fileName, QPixmap **pix){ taskList.append(QPair<QString, QPixmap**>(fileName,pix)); if(!worked) { worked = true; QPair<QString, QPixmap**> param = taskList.takeFirst(); pixSave = param.second; future = QtConcurrent::run(this, &CacheDisk::ReadInThread, param.first, param.second); futureWather.setFuture(future); connect(&futureWather, SIGNAL(finished()), this, SLOT(Finished()));// ReadInThread(param.first, param.second); }}void CacheDisk::Finished(){ *pixSave = future.result(); qDebug()<<"finish"<<*pixSave; QLabel *l = new QLabel; l->setPixmap(**pixSave); l->show(); worked = false; if(!taskList.empty()) { worked = true; QPair<QString, QPixmap**> param = taskList.takeFirst(); QtConcurrent::run(this, &CacheDisk::ReadInThread, param.first, param.second); }}QPixmap* CacheDisk::ReadInThread(QString fileName, QPixmap **pix){ QPixmap *tmpPixmap = new QPixmap(fileName);qDebug()<<tmpPixmap; return tmpPixmap;}
#ifndef CACHEDISK_H#define CACHEDISK_H#include <QObject>#include <QtConcurrentRun>#include <QImage>#include <QPixmap>#include <QLabel>#include <QFutureWatcher>class CacheDisk: public QObject{ Q_OBJECT QPixmap **pixSave; QList<QPair<QString,QPixmap**> > taskList; QPixmap *ReadInThread(QString fileName, QPixmap **pix); bool worked; QFuture<QPixmap*> future; QFutureWatcher<QPixmap*> futureWather;private slots: void Finished();public: CacheDisk(); void Read(QString fileName, QPixmap **pix);};#endif // CACHEDISK_H
#include <QApplication>#include <QPixmapCache>//#include "zzz.h"//#include "diskcache.h"#include "cachedisk.h"#include <QtGui>int main(int argc, char *argv[]){ QApplication app(argc, argv); CacheDisk cacheDisk; QPixmap *pix = 0; cacheDisk.Read("D:/WORK/learn/1.jpg", &pix); return app.exec();}