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 used to call function pointer
Qt 6.11 is out! See what's new in the release blog

QThread used to call function pointer

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.2k Views 2 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.
  • P Offline
    P Offline
    Pierre-Emmanuel
    wrote on last edited by
    #1

    Hi guys,

    I am developing a user interface which will need to run some code in a dedicated thread. I don't want to use the graphical thread because the source code execution may last few minutes to 20-30 minutes and the user interface will freeze.

    So far, I am using a sub class of QThread, redefining the method run and job done. However, I would like to have a generic "Working thread" which is taking a function pointer in the constructor and the run function is simply calling the function pointer.

    Here is a simple demo app:

    working_thread.h:
    231ba727-f2d6-4c07-b24d-83a50d6e861d-image.png

    working_thread.c:
    69c134b7-9e8e-4813-98b1-127f96da286b-image.png

    A dummy object to run some code asynchronously:
    5fdcb47c-2b15-472b-af67-bd218636b4ba-image.png

    But the error I got is the following:
    a98327f7-0ff2-4667-b3a4-d9575abc04bf-image.png

    I am a java developer and I can use lambda expression to avoid creating a function attached to an object. I investigated this direction, but each time I got compilation errors.

    What am I doing wrong here ?

    I thought the typedef bool (*Runnable)(); declared in the working_thread.h file was generic enough to accept all kind of functions as soon as it has the correct signature.

    I saw as well the moveToThread function, but I'm not sure if it is what I am looking for.

    Have a nice day,
    Pierre-Emmanuel

    jsulmJ 1 Reply Last reply
    0
    • P Pierre-Emmanuel

      Hi guys,

      I am developing a user interface which will need to run some code in a dedicated thread. I don't want to use the graphical thread because the source code execution may last few minutes to 20-30 minutes and the user interface will freeze.

      So far, I am using a sub class of QThread, redefining the method run and job done. However, I would like to have a generic "Working thread" which is taking a function pointer in the constructor and the run function is simply calling the function pointer.

      Here is a simple demo app:

      working_thread.h:
      231ba727-f2d6-4c07-b24d-83a50d6e861d-image.png

      working_thread.c:
      69c134b7-9e8e-4813-98b1-127f96da286b-image.png

      A dummy object to run some code asynchronously:
      5fdcb47c-2b15-472b-af67-bd218636b4ba-image.png

      But the error I got is the following:
      a98327f7-0ff2-4667-b3a4-d9575abc04bf-image.png

      I am a java developer and I can use lambda expression to avoid creating a function attached to an object. I investigated this direction, but each time I got compilation errors.

      What am I doing wrong here ?

      I thought the typedef bool (*Runnable)(); declared in the working_thread.h file was generic enough to accept all kind of functions as soon as it has the correct signature.

      I saw as well the moveToThread function, but I'm not sure if it is what I am looking for.

      Have a nice day,
      Pierre-Emmanuel

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Pierre-Emmanuel said in QThread used to call function pointer:

      I thought the typedef bool (*Runnable)(); declared in the working_thread.h file was generic enough to accept all kind of functions as soon as it has the correct signature.

      No, it's not. You're trying to use a pointer to a method of a class. Your Runnable is a simple function pointer.
      Take a look at https://opensource.com/article/21/2/ccc-method-pointers

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #3

        Subclassing Thread is in 99% of all cases the wrong approach. But you're coming from Java so I can't fault you for that :p

        What you're looking for is QtConcurrent, it accepts a function pointer and runs that in a different thread from an internal thread pool

        https://doc.qt.io/qt-6/qtconcurrent.html#run

        make sure your function is truly standalone, no access to shared objects etc


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        2
        • P Offline
          P Offline
          Pierre-Emmanuel
          wrote on last edited by
          #4

          Hi @J-Hilk and @jsulm

          Thank you for this information. I am trying now to use QtConcurrent::run function but I am facing an issue to be notified after the execution, to get a success status.

          Here is what I have done:

          async_caller.h:
          7b89a870-6509-498e-8e97-53013ea0dc8c-image.png

          async_caller.c:
          3915ce81-de93-4682-97ad-e4433dbb5f3f-image.png

          main function:
          5cc7f020-0d06-4018-a983-1a585c81cebf-image.png

          The output is:
          a7b1aa46-0e9f-4685-b7a5-cd870edc8d80-image.png

          I can see that the task is executed in a different thread, but somehow the slot onAsyncCallComplete is not called.

          I watch this youtube tutorial: https://www.youtube.com/watch?v=W9nWyTzxLjs

          and added the call to the deleteLater slot to be sure the watcher still exists when the callback should be executed.

          PS @J-Hilk: In Java I would not extends the Thread class but use a function to give to the thread constructor ahah. On a more serious topic, would it be a good idea to create a WorkingThread with a lambda expression as input argument for the constructor ?

          Have a nice day guys !
          Pierre-Emmanuel

          jsulmJ 1 Reply Last reply
          0
          • P Pierre-Emmanuel

            Hi @J-Hilk and @jsulm

            Thank you for this information. I am trying now to use QtConcurrent::run function but I am facing an issue to be notified after the execution, to get a success status.

            Here is what I have done:

            async_caller.h:
            7b89a870-6509-498e-8e97-53013ea0dc8c-image.png

            async_caller.c:
            3915ce81-de93-4682-97ad-e4433dbb5f3f-image.png

            main function:
            5cc7f020-0d06-4018-a983-1a585c81cebf-image.png

            The output is:
            a7b1aa46-0e9f-4685-b7a5-cd870edc8d80-image.png

            I can see that the task is executed in a different thread, but somehow the slot onAsyncCallComplete is not called.

            I watch this youtube tutorial: https://www.youtube.com/watch?v=W9nWyTzxLjs

            and added the call to the deleteLater slot to be sure the watcher still exists when the callback should be executed.

            PS @J-Hilk: In Java I would not extends the Thread class but use a function to give to the thread constructor ahah. On a more serious topic, would it be a good idea to create a WorkingThread with a lambda expression as input argument for the constructor ?

            Have a nice day guys !
            Pierre-Emmanuel

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Pierre-Emmanuel Please post code as text, not pictures.
            You do not have Qt event loop running, that's probably why your slot is not executed: msleep blocks the thread and just after that you terminate the application.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            3
            • P Offline
              P Offline
              Pierre-Emmanuel
              wrote on last edited by
              #6

              Hi @jsulm,

              I updated the main function to initialize the EventLoop it is working as expected.

              Thank you very much for your help,
              Pierre-Emmanuel de Robien

              1 Reply Last reply
              1
              • P Pierre-Emmanuel has marked this topic as solved on

              • Login

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