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. start and programmatically stop a thread
Forum Updated to NodeBB v4.3 + New Features

start and programmatically stop a thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 5 Posters 1.2k 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.
  • gbettegaG Offline
    gbettegaG Offline
    gbettega
    wrote last edited by
    #5

    ok thank you very much.
    Terminate(); the function within the thread does not used shared resources, and does not read/write to/from disk
    Have a nice day
    G

    JonBJ 1 Reply Last reply
    0
    • gbettegaG gbettega

      ok thank you very much.
      Terminate(); the function within the thread does not used shared resources, and does not read/write to/from disk
      Have a nice day
      G

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote last edited by
      #6

      @gbettega
      That is what I meant by

      You might "kill" it forcefully, but almost certainly that will leave things in a bad state.

      As per docs:

      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.

      Use at your own risk!

      gbettegaG 1 Reply Last reply
      1
      • JonBJ JonB

        @gbettega
        That is what I meant by

        You might "kill" it forcefully, but almost certainly that will leave things in a bad state.

        As per docs:

        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.

        Use at your own risk!

        gbettegaG Offline
        gbettegaG Offline
        gbettega
        wrote last edited by
        #7

        @JonB Thank you!
        What does exactly "bad state" means?
        Grazie mille
        Giovanni

        Pl45m4P JonBJ 2 Replies Last reply
        0
        • gbettegaG gbettega

          @JonB Thank you!
          What does exactly "bad state" means?
          Grazie mille
          Giovanni

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote last edited by Pl45m4
          #8

          @gbettega said in start and programmatically stop a thread:

          What does exactly "bad state" means?

          UB (undefined behavior), for example...
          If you stop/terminate the thread while processing it might leave garbage values behind, which then cause errors elsewhere.
          You can never be sure that everything is working as it should after "killing" the thread forcefully.
          terminate() IS a solution, but not a good one.

          All depends on what your thread/your app is doing.

          @gbettega said in start and programmatically stop a thread:

          the thread does not used shared resources, and does not read/write to/from disk

          It could potentially work for you... but could also result in errors later.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          3
          • gbettegaG gbettega

            @JonB Thank you!
            What does exactly "bad state" means?
            Grazie mille
            Giovanni

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote last edited by JonB
            #9

            @gbettega
            All as @Pl45m4 has written. It means "It works OK, if you're lucky, till some time it does not, if you are unlucky".

            Depending on what happens to happen when, you can kill it lots of times with no ill effects, then perchance one time it gets killed at a different state and that does have an effect. It does help mitigation that your thread at least "does not access anything else", but there are other things which can be affected.

            What is it that this DLL code does which takes so long to compute and does not allow for interruption/termination? Would you consider, maybe, that when user asks for "stop" you allow the thread to run to normal termination but disconnect/ignore any signals or output it sends you? That might be safer than killing.

            gbettegaG 1 Reply Last reply
            0
            • JonBJ JonB

              @gbettega
              All as @Pl45m4 has written. It means "It works OK, if you're lucky, till some time it does not, if you are unlucky".

              Depending on what happens to happen when, you can kill it lots of times with no ill effects, then perchance one time it gets killed at a different state and that does have an effect. It does help mitigation that your thread at least "does not access anything else", but there are other things which can be affected.

              What is it that this DLL code does which takes so long to compute and does not allow for interruption/termination? Would you consider, maybe, that when user asks for "stop" you allow the thread to run to normal termination but disconnect/ignore any signals or output it sends you? That might be safer than killing.

              gbettegaG Offline
              gbettegaG Offline
              gbettega
              wrote last edited by
              #10

              @JonB thank you for your reply.
              The code sent to the thread performs a geometry/topology operation performed on a BREP file. E.g. a defeatuting operation done on a complex shape geometry. It is one among the typical operations a geometry kernel of a CAD software performs onto n input geometry. If the required simplification is "simple" the dll takes a fraction of second, or less, but, if the defeaturing operation is made of multiple simplifications onto a complex shape, or multiple shapes, the kernel (here OpenCascade) could take serveral minutes, and in the meantime the GUI should remain unlocked. There is also the possibility that the code inside the library gets stuck: the user cannot distinguish between a stuck state (infinite loop) or a very long operation, so he generally "quits". Ignoring all it is happening in this case is not feasible, since these operations when running are intensive, both in terms of memory, both in terms of CPU.
              Have a nice day
              GIovanni

              JonBJ 1 Reply Last reply
              0
              • gbettegaG gbettega

                @JonB thank you for your reply.
                The code sent to the thread performs a geometry/topology operation performed on a BREP file. E.g. a defeatuting operation done on a complex shape geometry. It is one among the typical operations a geometry kernel of a CAD software performs onto n input geometry. If the required simplification is "simple" the dll takes a fraction of second, or less, but, if the defeaturing operation is made of multiple simplifications onto a complex shape, or multiple shapes, the kernel (here OpenCascade) could take serveral minutes, and in the meantime the GUI should remain unlocked. There is also the possibility that the code inside the library gets stuck: the user cannot distinguish between a stuck state (infinite loop) or a very long operation, so he generally "quits". Ignoring all it is happening in this case is not feasible, since these operations when running are intensive, both in terms of memory, both in terms of CPU.
                Have a nice day
                GIovanni

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote last edited by
                #11

                @gbettega
                I understand. Then you seem to have little choice other than to "kill/terminate" the thread. Hopefully in practice it will go OK.

                1 Reply Last reply
                0
                • jeremy_kJ Offline
                  jeremy_kJ Offline
                  jeremy_k
                  wrote last edited by
                  #12

                  A safer option is to run the calculation in a separate process. Termination there is easier to understand for a seasoned programmer.

                  Asking a question about code? http://eel.is/iso-c++/testcase/

                  JonBJ 1 Reply Last reply
                  2
                  • jeremy_kJ jeremy_k

                    A safer option is to run the calculation in a separate process. Termination there is easier to understand for a seasoned programmer.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote last edited by
                    #13

                    @jeremy_k
                    I had thought about suggesting that to the OP. But they will then need to write some IPC code to get the information back to the calling process which may be a bit of work, depending on the quantity and format of the results.

                    1 Reply Last reply
                    0
                    • jeremy_kJ Offline
                      jeremy_kJ Offline
                      jeremy_k
                      wrote last edited by
                      #14

                      Stdin and stdout of the child process work for low volumes. Shared memory for large data sets. It's unusual to want a multithreaded algorithm and not understand some form of IPC.

                      The risk of having a memory allocation abandoned, or a library dependency leaving a mutex locked dissuades me from considering QThread::terminate() in any circumstance.

                      Asking a question about code? http://eel.is/iso-c++/testcase/

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

                        Like previously stated, the only real option you have is to start a separate process from your Qt application. This process loads the DLL and performs the calculations.

                        You can then terminate that process at any time, and the operating system will take care of cleaning up memory, handles, and other resources. The only potential downside is the risk of corrupted files, if the DLL performs file operations.

                        Communication between your main application and the second process can be handled via QSharedMemory, QLocalSocket, or standard input/output piping.


                        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
                        0

                        • Login

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