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. How to cancel a long running API call running in a separate thread?
QtWS25 Last Chance

How to cancel a long running API call running in a separate thread?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 2.4k 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.
  • B Offline
    B Offline
    Bob1
    wrote on last edited by
    #1

    Hi -

    I'm looking for the best way to use QThreads to call a function that will take a long time in a separate thread whilst being able to stop the thread from the UI if the user wants to cancel the operation.

    I've looked online and it seems the general idea is to add a boolean flag to the long running function and check every so often returning if needed but as the function is part of a library I cannot do this. I need the function to run in a different thread and to have the ability to cancel it allowing it to free its resources much like Ctrl-C would on the command line.

    Is there any way to do this using Qt or perhaps something from std?

    Cheers.

    jsulmJ JonBJ 2 Replies Last reply
    0
    • B Bob1

      Hi -

      I'm looking for the best way to use QThreads to call a function that will take a long time in a separate thread whilst being able to stop the thread from the UI if the user wants to cancel the operation.

      I've looked online and it seems the general idea is to add a boolean flag to the long running function and check every so often returning if needed but as the function is part of a library I cannot do this. I need the function to run in a different thread and to have the ability to cancel it allowing it to free its resources much like Ctrl-C would on the command line.

      Is there any way to do this using Qt or perhaps something from std?

      Cheers.

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

      @Bob1 You can terminate the thread using http://doc.qt.io/qt-5/qthread.html#terminate
      But depending on what this API call is doing it can have negative impacts!

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

      1 Reply Last reply
      3
      • B Bob1

        Hi -

        I'm looking for the best way to use QThreads to call a function that will take a long time in a separate thread whilst being able to stop the thread from the UI if the user wants to cancel the operation.

        I've looked online and it seems the general idea is to add a boolean flag to the long running function and check every so often returning if needed but as the function is part of a library I cannot do this. I need the function to run in a different thread and to have the ability to cancel it allowing it to free its resources much like Ctrl-C would on the command line.

        Is there any way to do this using Qt or perhaps something from std?

        Cheers.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @Bob1 said in How to cancel a long running API call running in a separate thread?:

        allowing it to free its resources

        You can terminate thread as @jsulm says. The problem is: how do you know what that thread/API has done so far, how do you know what non-thread resources it may have created and not cleaned-up/released?! Unless the API itself offers a "cancel" function, or you know just what it does, you take your life in your own hands! :)

        1 Reply Last reply
        2
        • B Offline
          B Offline
          Bob1
          wrote on last edited by
          #4

          @jsulm @JonB

          A tricky problem it seems. I think the API call in this instance is self contained as in it will create resources of it's own but it won't modify anything outside of the thread I presumed these would all be taken care of when the thread is stopped but maybe this isn't the case...

          So there is no 'good' way to do this? I am surprised as it seems like it would perhaps be quite a good thing to have but maybe it causes more issues than it solves/there are better ways of doing it!

          Thanks for your responses.

          JonBJ jsulmJ JKSHJ kshegunovK 4 Replies Last reply
          0
          • B Bob1

            @jsulm @JonB

            A tricky problem it seems. I think the API call in this instance is self contained as in it will create resources of it's own but it won't modify anything outside of the thread I presumed these would all be taken care of when the thread is stopped but maybe this isn't the case...

            So there is no 'good' way to do this? I am surprised as it seems like it would perhaps be quite a good thing to have but maybe it causes more issues than it solves/there are better ways of doing it!

            Thanks for your responses.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @Bob1
            If the thread truly does only use its own, internal, "thread-local" resources, you may indeed be fine terminating it. But only you, or the API authors, know that!

            http://doc.qt.io/qt-5/qthread.html#terminate:

            Warning: This function is dangerous and its use is discouraged. The thread can be terminated at any point in its code path. Threads can be terminated while modifying data. There is no chance for the thread to clean up after itself, unlock any held mutexes, etc. In short, use this function only if absolutely necessary.

            If it only modifies its own data, and doesn't interact with the OS, you should be OK. But don't blame us if it turns out to be halfway through writing to a file! :)

            Note: one difference from your Ctrl+C example is that a program can trap that signal and clean up nicely. Terminating a thread does not allow that. Think of it more like using Task Manager End Task or kill -9 on a process.

            1 Reply Last reply
            1
            • B Bob1

              @jsulm @JonB

              A tricky problem it seems. I think the API call in this instance is self contained as in it will create resources of it's own but it won't modify anything outside of the thread I presumed these would all be taken care of when the thread is stopped but maybe this isn't the case...

              So there is no 'good' way to do this? I am surprised as it seems like it would perhaps be quite a good thing to have but maybe it causes more issues than it solves/there are better ways of doing it!

              Thanks for your responses.

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

              @Bob1 said in How to cancel a long running API call running in a separate thread?:

              I presumed these would all be taken care of when the thread is stopped

              How? If you terminate the thread then it is terminated, only you know what you do in the thread and it is your job to clean up, there is no magic.

              "So there is no 'good' way to do this?" - the good way would be to have an API which allows to interrupt long lasting operations in a clean way...

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

              1 Reply Last reply
              1
              • B Bob1

                @jsulm @JonB

                A tricky problem it seems. I think the API call in this instance is self contained as in it will create resources of it's own but it won't modify anything outside of the thread I presumed these would all be taken care of when the thread is stopped but maybe this isn't the case...

                So there is no 'good' way to do this? I am surprised as it seems like it would perhaps be quite a good thing to have but maybe it causes more issues than it solves/there are better ways of doing it!

                Thanks for your responses.

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                @Bob1 said in How to cancel a long running API call running in a separate thread?:

                I think the API call in this instance is self contained as in it will create resources of it's own but it won't modify anything outside of the thread I presumed these would all be taken care of when the thread is stopped but maybe this isn't the case...

                A well-designed API should not block for an extended period of time. Instead, it should provide a proper way for you to "abort"/"cancel" the current operation and clean up its resources.

                If the API is not well-designed, then you're stuck. You can either wait until it finishes, or you can terminate it and pray that the termination doesn't break anything.

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

                1 Reply Last reply
                4
                • B Bob1

                  @jsulm @JonB

                  A tricky problem it seems. I think the API call in this instance is self contained as in it will create resources of it's own but it won't modify anything outside of the thread I presumed these would all be taken care of when the thread is stopped but maybe this isn't the case...

                  So there is no 'good' way to do this? I am surprised as it seems like it would perhaps be quite a good thing to have but maybe it causes more issues than it solves/there are better ways of doing it!

                  Thanks for your responses.

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

                  @JKSH said in How to cancel a long running API call running in a separate thread?:

                  A well-designed API should not block for an extended period of time.

                  Or at least should provide a way to set timeout for a blocking operation, which many APIs unfortunately don't; so what @JKSH wrote.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  1
                  • B Offline
                    B Offline
                    Bob1
                    wrote on last edited by
                    #9

                    @JonB @jsulm @JKSH @kshegunov

                    Thanks for all your insight guys I really appreciate it.

                    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