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

QThread * create(Function &&f)



  • Hello. I want to create a new thread using QThread * create(Function &&f). But I don't know how should transfer parameter. I don't quite understand this parameter-Function &&f. Here is my code.

    class a
    {
    private:
    QThread* XferThread;
    void XferLoop();
    }
    a::a
    {
    XferThread = QThread::create(XferLoop);
    }
    

    Parameter list mismatch. Does that have anything to do with member function(XferLoop)?How should I change my code?Thanks for any reply.
    VS2015 + Qt5.12 on Windows10.


  • Lifetime Qt Champion

    Hi,

    Based on the documentation of the create method, something like:

    // somewhere:
    void myFunction(int arg1, int arg2)
    {
        qDebug() << Q_FUNC_INFO << arg1 << arg2;
    }
    
    // somewhere suitable:
    QThread *myThread = QThread::create(myFunction, 12, 30);
    


  • @qt_crazyer

    Note: this function is only available when using C++17.

    Does VS2015 support C++17?
    If yes, did you enabled it with CONFIG+=c++17 in the .pro file?



  • @sgaist
    Thanks for your reply.
    The example you mentioned is correct. But in my code, XferLoop() is the member function of class a. So it shows the error-Parameter list mismatch. The function XferLoop() calls many private variables of class a.
    QThread::create(Function &&f) dosen't need C++17, but the QThread::create(Function &&f, Args &&... args) does.



  • @qt_crazyer said in QThread * create(Function &&f):

    QThread::create(Function &&f) dosen't need C++17, but the QThread::create(Function &&f, Args &&... args) does.

    Sorry my fault!



  • a::a
    :XferThread(QThread::create(std::bind(&a::XferLoop,this))
    {
    
    }
    

    The function XferLoop() calls many private variables of class a.

    hic sunt leones. Be very careful not to create race conditions


Log in to reply