Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. Pausing a QThread
QtWS25 Last Chance

Pausing a QThread

Scheduled Pinned Locked Moved Unsolved Brainstorm
11 Posts 5 Posters 2.5k Views
  • 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.
  • J Offline
    J Offline
    JKSH
    Moderators
    wrote on 8 Jun 2022, 08:57 last edited by
    #2

    What kind of pausing do you have in mind?

    • Pause execution in the middle of a function call? OR
    • Finish processing the current event, and then pause the processing of future events?

    The latter should be quite simple to implement yourself. I'm not sure if the former is even possible in C++.

    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

    J J 2 Replies Last reply 8 Jun 2022, 09:01
    0
    • J JKSH
      8 Jun 2022, 08:57

      What kind of pausing do you have in mind?

      • Pause execution in the middle of a function call? OR
      • Finish processing the current event, and then pause the processing of future events?

      The latter should be quite simple to implement yourself. I'm not sure if the former is even possible in C++.

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 8 Jun 2022, 09:01 last edited by J.Hilk 6 Aug 2022, 09:04
      #3

      @JKSH said in Pausing a QThread:

      I'm not sure if the former is even possible in C++

      it should be now with Coroutines (C++20) :D


      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.

      J 1 Reply Last reply 9 Jun 2022, 02:45
      0
      • J JKSH
        8 Jun 2022, 08:57

        What kind of pausing do you have in mind?

        • Pause execution in the middle of a function call? OR
        • Finish processing the current event, and then pause the processing of future events?

        The latter should be quite simple to implement yourself. I'm not sure if the former is even possible in C++.

        J Offline
        J Offline
        JonB
        wrote on 8 Jun 2022, 09:22 last edited by JonB 6 Aug 2022, 09:23
        #4

        @JKSH said in Pausing a QThread:

        The latter should be quite simple to implement yourself. I'm not sure if the former is even possible in C++.

        ?

        I'm beginning to see my expectations may have been incorrect....

        Windows first. It has (https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-suspendthread):

        DWORD SuspendThread(
          [in] HANDLE hThread
        );
        

        allowing one thread to suspend another. However I do now note:

        This function is primarily designed for use by debuggers.

        Linux next. I assumed there would be e.g. a pthread_pause/_resume() call, but it seems not.

        For a process, as you know e.g. from a terminal you can type Ctrl+Z to suspend the process. That will send a SIGSTOP to the process, and later a SIGCONT to resume it. If this can be done for processes I thought it could be done for threads....

        Let's say: I have code to compute the Ackermann function (or similar process-intensive computation) :) I include this in my Qt project, but the author did not write it with a thread in mind and it does not check some state or interruption or pause flag as it goes. I do not want touch its source code.

        Now I notice that as it runs it fully occupies a core and my computer is getting hot! Perhaps the user does something in the UI where he might be going to specify a new number to calculate it for, and while waiting for him to enter the new number I want to stop the processor overheating if he is going to cause it to need to restart but we don't know yet, so we want to pause where it is now before either continuing or stopping it.

        Surely these OSes have the architecture to e.g. tell the OS scheduler to not longer allocate any time slices to a given thread for a while? Or not??

        J 1 Reply Last reply 8 Jun 2022, 09:34
        0
        • J JonB
          8 Jun 2022, 09:22

          @JKSH said in Pausing a QThread:

          The latter should be quite simple to implement yourself. I'm not sure if the former is even possible in C++.

          ?

          I'm beginning to see my expectations may have been incorrect....

          Windows first. It has (https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-suspendthread):

          DWORD SuspendThread(
            [in] HANDLE hThread
          );
          

          allowing one thread to suspend another. However I do now note:

          This function is primarily designed for use by debuggers.

          Linux next. I assumed there would be e.g. a pthread_pause/_resume() call, but it seems not.

          For a process, as you know e.g. from a terminal you can type Ctrl+Z to suspend the process. That will send a SIGSTOP to the process, and later a SIGCONT to resume it. If this can be done for processes I thought it could be done for threads....

          Let's say: I have code to compute the Ackermann function (or similar process-intensive computation) :) I include this in my Qt project, but the author did not write it with a thread in mind and it does not check some state or interruption or pause flag as it goes. I do not want touch its source code.

          Now I notice that as it runs it fully occupies a core and my computer is getting hot! Perhaps the user does something in the UI where he might be going to specify a new number to calculate it for, and while waiting for him to enter the new number I want to stop the processor overheating if he is going to cause it to need to restart but we don't know yet, so we want to pause where it is now before either continuing or stopping it.

          Surely these OSes have the architecture to e.g. tell the OS scheduler to not longer allocate any time slices to a given thread for a while? Or not??

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 8 Jun 2022, 09:34 last edited by
          #5

          @JonB said in Pausing a QThread:

          Surely these OSes have the architecture to e.g. tell the OS scheduler to not longer allocate any time slices to a given thread for a while? Or not??

          isn't that what sleep() does ?


          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.

          J 1 Reply Last reply 8 Jun 2022, 09:49
          0
          • J J.Hilk
            8 Jun 2022, 09:34

            @JonB said in Pausing a QThread:

            Surely these OSes have the architecture to e.g. tell the OS scheduler to not longer allocate any time slices to a given thread for a while? Or not??

            isn't that what sleep() does ?

            J Offline
            J Offline
            JonB
            wrote on 8 Jun 2022, 09:49 last edited by
            #6

            @J-Hilk
            sleep() has to be called within the process/thread, which has to know it wants to sleep/pause!
            My question is about another thread suspending a thread it created, that's the whole point....

            1 Reply Last reply
            0
            • J J.Hilk
              8 Jun 2022, 09:01

              @JKSH said in Pausing a QThread:

              I'm not sure if the former is even possible in C++

              it should be now with Coroutines (C++20) :D

              J Offline
              J Offline
              JKSH
              Moderators
              wrote on 9 Jun 2022, 02:45 last edited by
              #7

              @J-Hilk said in Pausing a QThread:

              it should be now with Coroutines (C++20) :D

              If I've understood it correctly, coroutines still need to be written in a way such that the routine pauses itself at well-defined spots?

              This is not the same as an external actor suspending a function at any arbitrary spot.

              @JonB said in Pausing a QThread:

              Let's say: I have code to compute the Ackermann function (or similar process-intensive computation) :) I include this in my Qt project, but the author did not write it with a thread in mind and it does not check some state or interruption or pause flag as it goes. I do not want touch its source code.

              Then you file a bug report/feature request and ask the author to touch the source code :-)

              Surely these OSes have the architecture to e.g. tell the OS scheduler to not longer allocate any time slices to a given thread for a while? Or not??

              I have not seen such a feature. I'd be most interested if someone can find an example.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              J 1 Reply Last reply 9 Jun 2022, 07:05
              0
              • J JKSH
                9 Jun 2022, 02:45

                @J-Hilk said in Pausing a QThread:

                it should be now with Coroutines (C++20) :D

                If I've understood it correctly, coroutines still need to be written in a way such that the routine pauses itself at well-defined spots?

                This is not the same as an external actor suspending a function at any arbitrary spot.

                @JonB said in Pausing a QThread:

                Let's say: I have code to compute the Ackermann function (or similar process-intensive computation) :) I include this in my Qt project, but the author did not write it with a thread in mind and it does not check some state or interruption or pause flag as it goes. I do not want touch its source code.

                Then you file a bug report/feature request and ask the author to touch the source code :-)

                Surely these OSes have the architecture to e.g. tell the OS scheduler to not longer allocate any time slices to a given thread for a while? Or not??

                I have not seen such a feature. I'd be most interested if someone can find an example.

                J Offline
                J Offline
                JonB
                wrote on 9 Jun 2022, 07:05 last edited by
                #8

                @JKSH said in Pausing a QThread:

                Then you file a bug report/feature request and ask the author to touch the source code :-)

                Sorry, not fair. When I write a function to do Ackermann function, I just write it to do the steps. I do not go "Maybe someone will want to run this in a thread in the future and would like me to check on each iteration whether to pause or not".

                I have not seen such a feature. I'd be most interested if someone can find an example.

                That is what e.g. Linux pause(2), sigsuspend(2) or the SIGSTOP signal all do. They all suspend a process/thread, it does not get any further time splices from the scheduler till the resumption signal is deleivered?

                S 1 Reply Last reply 9 Jun 2022, 07:24
                0
                • J JonB
                  9 Jun 2022, 07:05

                  @JKSH said in Pausing a QThread:

                  Then you file a bug report/feature request and ask the author to touch the source code :-)

                  Sorry, not fair. When I write a function to do Ackermann function, I just write it to do the steps. I do not go "Maybe someone will want to run this in a thread in the future and would like me to check on each iteration whether to pause or not".

                  I have not seen such a feature. I'd be most interested if someone can find an example.

                  That is what e.g. Linux pause(2), sigsuspend(2) or the SIGSTOP signal all do. They all suspend a process/thread, it does not get any further time splices from the scheduler till the resumption signal is deleivered?

                  S Offline
                  S Offline
                  SimonSchroeder
                  wrote on 9 Jun 2022, 07:24 last edited by
                  #9

                  @JonB said in Pausing a QThread:

                  That is what e.g. Linux pause(2), sigsuspend(2) or the SIGSTOP signal all do.

                  And then again pause() and sigsuspend() only pause the current thread and cannot be used to pause a different thread (which is the actual question). Maybe one can deliver SIGSTOP to a different thread?

                  J 1 Reply Last reply 9 Jun 2022, 07:37
                  0
                  • S SimonSchroeder
                    9 Jun 2022, 07:24

                    @JonB said in Pausing a QThread:

                    That is what e.g. Linux pause(2), sigsuspend(2) or the SIGSTOP signal all do.

                    And then again pause() and sigsuspend() only pause the current thread and cannot be used to pause a different thread (which is the actual question). Maybe one can deliver SIGSTOP to a different thread?

                    J Offline
                    J Offline
                    JonB
                    wrote on 9 Jun 2022, 07:37 last edited by
                    #10

                    @SimonSchroeder said in Pausing a QThread:

                    And then again pause() and sigsuspend() only pause the current thread and cannot be used to pause a different thread (which is the actual question)

                    Absolutely. The question (from @JKSH) was what could do this.

                    Maybe one can deliver SIGSTOP to a different thread?

                    Absolutely you can. E.g. as I said this (or SIGTSTP) is what is happening when you Ctrl+Z etc. from a shell.

                    My point was that the OS thread/process time-slice scheduler can do all of this, so the facilities for "pausing" must be there.

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      Kent-Dorfman
                      wrote on 10 Jun 2022, 03:23 last edited by Kent-Dorfman 6 Oct 2022, 03:26
                      #11

                      The broader argument is that pausing a thread is a misuse of them. Also, you need to consider that the qt thread abstraction may not be a true system level thread, but instead a pseudo-thread that supports the common threading api and behaviour (to some extent).

                      As for pause/yield/etc as in coroutines...seems like it's a 20 year full circle back to the era of cooperative multi-tasking, when the favored model has clearly become preemptive MT.

                      Seems to me that the hint about how qt threads exist would be whether
                      std::this_thread::sleep_for() works as expected, or does it introduce side-effects?

                      1 Reply Last reply
                      0

                      11/11

                      10 Jun 2022, 03:23

                      • Login

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