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. Restarting multiple QThreads results in a crash
Forum Updated to NodeBB v4.3 + New Features

Restarting multiple QThreads results in a crash

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 5 Posters 1.8k 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.
  • Ronel_qtmasterR Ronel_qtmaster

    @Plastic-Jesus I will just give some hints

    • When you call QThread::start() it calls the function run() of QThread class.so you should reimplement the function run() not start()
    • you should always consider deleting the thread when it has finished
    • When you run many threads on the same time, it can results in unsufficient memory to execute them.Therefore also think about that aspect.
    Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #8

    @Ronel_qtmaster said in Restarting multiple QThreads results in a crash:

    When you call QThread::start() it calls the function run() of QThread class.so you should reimplement the function run() not start()

    Please look at the code - QThread is not reimplemented anywhere so no need to override run()

    you should always consider deleting the thread when it has finished

    Why? You can reuse them, that's also the point of QThreadPool

    When you run many threads on the same time, it can results in unsufficient memory to execute them.Therefore also think about that aspect.

    It's a reproducer of the problem - no need to add additional error handling here for such kind of errors.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    Ronel_qtmasterR 1 Reply Last reply
    2
    • jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #9

      What's the goal of QThread reuse, other than avoiding repeat assignments of QObject thread affinity?

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

      Christian EhrlicherC 1 Reply Last reply
      1
      • jeremy_kJ jeremy_k

        What's the goal of QThread reuse, other than avoiding repeat assignments of QObject thread affinity?

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by Christian Ehrlicher
        #10

        @jeremy_k Creating a new QThread/std::thread can be time consuming - therefore QThreadPool. But yes in general I would at least not create such stuff by myself and simply use the classes/toools provided.
        The problem here is also not QThread but a race condition underneath.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        jeremy_kJ 1 Reply Last reply
        2
        • Christian EhrlicherC Christian Ehrlicher

          @Ronel_qtmaster said in Restarting multiple QThreads results in a crash:

          When you call QThread::start() it calls the function run() of QThread class.so you should reimplement the function run() not start()

          Please look at the code - QThread is not reimplemented anywhere so no need to override run()

          you should always consider deleting the thread when it has finished

          Why? You can reuse them, that's also the point of QThreadPool

          When you run many threads on the same time, it can results in unsufficient memory to execute them.Therefore also think about that aspect.

          It's a reproducer of the problem - no need to add additional error handling here for such kind of errors.

          Ronel_qtmasterR Offline
          Ronel_qtmasterR Offline
          Ronel_qtmaster
          wrote on last edited by
          #11

          @Christian-Ehrlicher I was just giving some hints about threads in general.Also, even if you want to reuse a thread the proper way is the delete it first.And even threadpool that you mentioned automatically deletes runnable objects after the execution before you can restart the threadpool.
          Please do not tell something is wrong without checking it.thread error.PNG
          Even the problem about memory you can see it above.And it creates crashes as well.

          Christian EhrlicherC 1 Reply Last reply
          0
          • Ronel_qtmasterR Ronel_qtmaster

            @Christian-Ehrlicher I was just giving some hints about threads in general.Also, even if you want to reuse a thread the proper way is the delete it first.And even threadpool that you mentioned automatically deletes runnable objects after the execution before you can restart the threadpool.
            Please do not tell something is wrong without checking it.thread error.PNG
            Even the problem about memory you can see it above.And it creates crashes as well.

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by Christian Ehrlicher
            #12

            @Ronel_qtmaster said in Restarting multiple QThreads results in a crash:

            even if you want to reuse a thread the proper way is the delete it first.

            That's not a re-use... you delete one and create a new one with all the overhead involved. And that's not how QThreadPool is working.

            Please do not tell something is wrong without checking it.

            You do not understand what a testcase/reproducer is for ...

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            Ronel_qtmasterR 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              @Ronel_qtmaster said in Restarting multiple QThreads results in a crash:

              even if you want to reuse a thread the proper way is the delete it first.

              That's not a re-use... you delete one and create a new one with all the overhead involved. And that's not how QThreadPool is working.

              Please do not tell something is wrong without checking it.

              You do not understand what a testcase/reproducer is for ...

              Ronel_qtmasterR Offline
              Ronel_qtmasterR Offline
              Ronel_qtmaster
              wrote on last edited by
              #13

              @Christian-Ehrlicher by reuse i mean a new thread object doing the same process.Even you asumed that we do not need to delete a thread after its use which is wrong.Even QThreadpool is doing that.I have shown you the proof that the use of many threads can create sometimes memory problems, therefore a crash.

              Christian EhrlicherC 1 Reply Last reply
              0
              • Ronel_qtmasterR Ronel_qtmaster

                @Christian-Ehrlicher by reuse i mean a new thread object doing the same process.Even you asumed that we do not need to delete a thread after its use which is wrong.Even QThreadpool is doing that.I have shown you the proof that the use of many threads can create sometimes memory problems, therefore a crash.

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by Christian Ehrlicher
                #14

                @Ronel_qtmaster said in Restarting multiple QThreads results in a crash:

                have shown you the proof that the use of many threads can create sometimes memory problems, therefore a crash.

                I don't know what you're trying to explain here - the crash the OP reports has nothing to do with too many threads. He is only creating ten and is reusing them. So what are you trying to tell us? That you can't create an indefinitly amount of objects without getting an oom situation? Thx I'm aware if this since 30 years of programming.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                Ronel_qtmasterR 1 Reply Last reply
                2
                • Christian EhrlicherC Christian Ehrlicher

                  @Ronel_qtmaster said in Restarting multiple QThreads results in a crash:

                  have shown you the proof that the use of many threads can create sometimes memory problems, therefore a crash.

                  I don't know what you're trying to explain here - the crash the OP reports has nothing to do with too many threads. He is only creating ten and is reusing them. So what are you trying to tell us? That you can't create an indefinitly amount of objects without getting an oom situation? Thx I'm aware if this since 30 years of programming.

                  Ronel_qtmasterR Offline
                  Ronel_qtmasterR Offline
                  Ronel_qtmaster
                  wrote on last edited by
                  #15

                  @Christian-Ehrlicher you earlier said that the information i wrote were false.Now you are saying you don't understand.

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #16

                    I just don't understand what your posts help to resolve the issue. The Maintainer of the Qt thread stuff already said it has nothing to do with the threads but it's a locking problem within QProperty handling. The testcase just reveals it in a very good and fast way.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    1
                    • Christian EhrlicherC Christian Ehrlicher

                      @jeremy_k Creating a new QThread/std::thread can be time consuming - therefore QThreadPool. But yes in general I would at least not create such stuff by myself and simply use the classes/toools provided.
                      The problem here is also not QThread but a race condition underneath.

                      jeremy_kJ Offline
                      jeremy_kJ Offline
                      jeremy_k
                      wrote on last edited by
                      #17

                      @Christian-Ehrlicher said in Restarting multiple QThreads results in a crash:

                      @jeremy_k Creating a new QThread/std::thread can be time consuming

                      That's an interesting comparison. std::thread starts executing when it is instantiated. It doesn't have the ability to separate the equivalent of QThread::QThread() from QThread::start().

                      At a glance, there's a significant amount of code in the platform-specific implementations of QThread::start(), leading me to wonder how much avoiding additional calls to QThread::QThread saves. The underlying platform thread is (re)created during each successful call to QThread::start() in the Windows and unix implementations.

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

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Christian Ehrlicher

                        @Plastic-Jesus said in Restarting multiple QThreads results in a crash:

                        Just to make sure that my understanding of QThread restartability is correct

                        It is correct. As Thiago told you you should rework your stuff to avoid the crash. Maybe use QThreadPool instead your own solution.

                        P Offline
                        P Offline
                        Plastic.Jesus
                        wrote on last edited by
                        #18

                        @Christian-Ehrlicher Just a quick note. I agree with everything you said. Regarding QThreadPool, the actual software does not use a thread pool like my demo testcase. It simply has threads that fire up periodically to perform some maintenance operations. I created the thread pool in the test code as the most succinct demonstration of the issue.

                        Thanks again.

                        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