Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] Write Dll's for Heavy work



  • Hi,

    I know how to create a dll. I want to write a dll that runs a process and it doesnt block the main application. I tried with QThreadPool and QRunnable, but it crashes. Something like:

    @
    #include "MyDll.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    MyDll dll;
    dll.heavyProcess();
    dll.heavyProcess();
    dll.heavyProcess();
    dll.heavyProcess();

    return app.exec();
    }
    @

    Any idea will be appreciated.

    Thanks


  • Lifetime Qt Champion

    Hi,

    What about QtConcurent ?

    Also, it would be better to fix the crash



  • Thanks...

    I've resolved like this:

    myDll.h

    @#ifndef myDll_h
    #define myDll_h

    #include <QObject>
    #include "myDllGlobal.h"

    class QThreadPool;

    class myDll_EXPORT myDll : public QObject
    {
    Q_OBJECT

    public:
    HttpLoggerDll(QObject *parent = 0);
    void heavyWork();

    private:
    static QThreadPool *threadPool;
    };
    #endif
    @

    myDll.cpp

    @
    #include "myDll.h"
    #include "MyThread.h"
    #include <QThreadPool>

    QThreadPool *myDll::threadPool = 0;

    myDll::myDll(QObject *parent) : QObject(parent)
    {
    threadPool = QThreadPool::globalInstance();
    }

    void myDll::heavyWork(){
    MyThread *thread = new MyThread();
    threadPool->start(thread);
    }
    @

    MyThread.h

    @
    #ifndef MyThread_h
    #define MyThread_h

    #include <QRunnable>
    #include <QObject>

    class MyThread :public QObject, public QRunnable{

    Q_OBJECT

    public:
    MyThread(QObject *parent = 0);
    ~MyThread();

    void run();
    

    };
    #endif
    @

    MyThread.cpp

    @#include "MyThread.h"

    MyThread::MyThread(QObject *parent) : QObject(parent) {}

    MyThread::~MyThread(){}

    void MyThread::run(){
    // heavy work
    }
    @


  • Lifetime Qt Champion

    So you have it fixed ?



  • Yes. it works. Thanks


  • Lifetime Qt Champion

    What did you do to make it work ?



  • Declaring my variable threadPool as static was the key


  • Lifetime Qt Champion

    Sounds rather like a workaround. Anyway, you shouldn't need to have that variable. You should rather use the "global instance":http://qt-project.org/doc/qt-5/qthreadpool.html#details


Log in to reply