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. QThread * create(Function &&f)
Forum Updated to NodeBB v4.3 + New Features

QThread * create(Function &&f)

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 7 Posters 3.6k Views 3 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.
  • Qt_crazyerQ Offline
    Qt_crazyerQ Offline
    Qt_crazyer
    wrote on last edited by
    #1

    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.

    beeckscheB 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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);
      

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Qt_crazyerQ 1 Reply Last reply
      3
      • Qt_crazyerQ Qt_crazyer

        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.

        beeckscheB Offline
        beeckscheB Offline
        beecksche
        wrote on last edited by beecksche
        #3

        @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?

        1 Reply Last reply
        0
        • SGaistS SGaist

          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_crazyerQ Offline
          Qt_crazyerQ Offline
          Qt_crazyer
          wrote on last edited by
          #4

          @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.

          beeckscheB 1 Reply Last reply
          0
          • Qt_crazyerQ Qt_crazyer

            @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.

            beeckscheB Offline
            beeckscheB Offline
            beecksche
            wrote on last edited by
            #5

            @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!

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6
              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

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              3
              • A Offline
                A Offline
                AshkanV
                wrote on last edited by
                #7

                You can also use a lambda which is preferred over std::bind:

                a::a():
                XferThread(QThread::create([this](){XferLoop();})
                {}
                
                kshegunovK 1 Reply Last reply
                0
                • A AshkanV

                  You can also use a lambda which is preferred over std::bind:

                  a::a():
                  XferThread(QThread::create([this](){XferLoop();})
                  {}
                  
                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #8

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

                  which is preferred over std::bind

                  One of them catch-all phrases, eh? Says who, and why is it supposedly preferred?

                  Read and abide by the Qt Code of Conduct

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • kshegunovK kshegunov

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

                    which is preferred over std::bind

                    One of them catch-all phrases, eh? Says who, and why is it supposedly preferred?

                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @kshegunov https://clang.llvm.org/extra/clang-tidy/checks/modernize-avoid-bind.html :)

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    kshegunovK 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @kshegunov https://clang.llvm.org/extra/clang-tidy/checks/modernize-avoid-bind.html :)

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #10

                      Okay, so I found out about the "who". Now if you can convince me that "it is preferred" is the same as "clang tidy says it's preferred" ...
                      I remember there was this one guy who said everything should be auto because reasons. Recently there was this other guy (here) that didn't realize using auto broke his code ... stuff happens, I guess ...

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

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

                        Now if you can convince me that "it is preferred" is the same as "clang tidy says it's preferred" ...

                        No, no need to convince you. :)
                        I prefer lambdas because I never understood std::bind() and I need to think about it every time I see such a call :)

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        kshegunovK 1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

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

                          Now if you can convince me that "it is preferred" is the same as "clang tidy says it's preferred" ...

                          No, no need to convince you. :)
                          I prefer lambdas because I never understood std::bind() and I need to think about it every time I see such a call :)

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on last edited by
                          #12

                          @Christian-Ehrlicher said in QThread * create(Function &&f):

                          I prefer lambdas

                          Such reasoning I can live with, even without any justification. I just grumble at the catch-it-all-no-need-to-think-about-it sort of statement(s).

                          Read and abide by the Qt Code of Conduct

                          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