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.
  • Q Offline
    Q Offline
    Qt_crazyer
    wrote on 18 Sept 2019, 13:59 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.

    B 1 Reply Last reply 18 Sept 2019, 14:32
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 18 Sept 2019, 14:19 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

      Q 1 Reply Last reply 18 Sept 2019, 14:57
      3
      • Q Qt_crazyer
        18 Sept 2019, 13:59

        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.

        B Offline
        B Offline
        beecksche
        wrote on 18 Sept 2019, 14:32 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
        • S SGaist
          18 Sept 2019, 14:19

          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);
          
          Q Offline
          Q Offline
          Qt_crazyer
          wrote on 18 Sept 2019, 14:57 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.

          B 1 Reply Last reply 18 Sept 2019, 15:37
          0
          • Q Qt_crazyer
            18 Sept 2019, 14:57

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

            B Offline
            B Offline
            beecksche
            wrote on 18 Sept 2019, 15:37 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
            • V Offline
              V Offline
              VRonin
              wrote on 18 Sept 2019, 15:50 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 31 Mar 2021, 10:10 last edited by
                #7

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

                a::a():
                XferThread(QThread::create([this](){XferLoop();})
                {}
                
                K 1 Reply Last reply 31 Mar 2021, 12:04
                0
                • A AshkanV
                  31 Mar 2021, 10:10

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

                  a::a():
                  XferThread(QThread::create([this](){XferLoop();})
                  {}
                  
                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 31 Mar 2021, 12:04 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 31 Mar 2021, 18:35
                  0
                  • K kshegunov
                    31 Mar 2021, 12:04

                    @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 Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 31 Mar 2021, 18:35 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

                    K 1 Reply Last reply 31 Mar 2021, 18:38
                    0
                    • Christian EhrlicherC Christian Ehrlicher
                      31 Mar 2021, 18:35

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

                      K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 31 Mar 2021, 18:38 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 Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 31 Mar 2021, 18:44 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

                        K 1 Reply Last reply 31 Mar 2021, 18:47
                        0
                        • Christian EhrlicherC Christian Ehrlicher
                          31 Mar 2021, 18:44

                          @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 :)

                          K Offline
                          K Offline
                          kshegunov
                          Moderators
                          wrote on 31 Mar 2021, 18:47 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