QtConCurrent::map always crash for simple multi-thread program
-
os : mac osx 10.8.3
compiler: clang3.2
Qt version: 5.1.hpp
@
#ifndef LOADIMAGES_HPP
#define LOADIMAGES_HPP#include <QMutex>
#include <QObject>class QString;
class QStringList;class loadImages : public QObject
{
Q_OBJECT
public:
explicit loadImages(QObject *parent = nullptr);
loadImages(loadImages const&) = delete;
loadImages& operator=(loadImages const&) = delete;private:
void loadImagesImpl_2(QStringList const &names);
void loadImagesImpl(QString const &name);private:
QMutex mutex_;
};#endif // LOADIMAGES_HPP
@.cpp
@
#include <functional>#include <QDebug>
#include <QtConcurrent/QtConcurrentMap>
#include <QtConcurrent/QtConcurrentRun>
#include <QDir>
#include <QImage>
#include <QMutexLocker>
#include <QStringList>#include "loadImages.hpp"
loadImages::loadImages(QObject *parent) : QObject(parent)
{
namespace ph = std::placeholders;
QStringList names;
names<<"/Users/yyyy/Downloads/1359170070532.jpg"<<"/Users/yyyy/Downloads/1370902954521.jpg"
<<"/Users/yyyy/Downloads/1370968889277.jpg"<<"/Users/yyyy/Downloads/1373155482891.jpg";for(auto const &data: names){ QImage img(data); qDebug()<<names<<" is null : "<<img.isNull(); } QThreadPool::globalInstance()->setMaxThreadCount(1); QtConcurrent::map(names, std::bind(&loadImages::loadImagesImpl, this, ph::_1)); //always crash //QtConcurrent::run(this, &loadImages::loadImagesImpl_2, names); //work fine
}
void loadImages::loadImagesImpl_2(QStringList const &names)
{
for(auto const &data: names){
QImage img(data);
}
}void loadImages::loadImagesImpl(QString const &name)
{
//I don't think we need a mutex at here, but I want to give this "workaround" a try
//no matter I add this mutex or not, the program always crash
QMutexLocker lock(&mutex_);QImage img(name);
}
@main.cpp
@
#include <QApplication>
#include <QLabel>#include "loadImages.hpp"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);loadImages load; QLabel label; label.setMinimumSize(400, 400); label.show(); return app.exec();
}
@
The program always crash if I call QtConcurrent::Map, but QtConcurrent::run work fine
What kind of faults do I make?Can't find any error