Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QtConCurrent::map always crash for simple multi-thread program
Forum Updated to NodeBB v4.3 + New Features

QtConCurrent::map always crash for simple multi-thread program

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 937 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stereomatching
    wrote on last edited by
    #1

    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(&#41;;
    

    }

    @

    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

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved