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. Thread with no event loop, detect an exit event
QtWS25 Last Chance

Thread with no event loop, detect an exit event

Scheduled Pinned Locked Moved Solved General and Desktop
event loopthreadsbest practice
6 Posts 4 Posters 3.1k 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.
  • N Offline
    N Offline
    Northsoft
    wrote on 21 Jun 2016, 08:27 last edited by
    #1

    Hi all, I write a program which has a separate thread with computation routines about 1..10..30 minutes long. So event loop is useless here. But I'd like to keep thread manageable so I'm wondering how working slot should query thread instance about posted termination event. Are there any best practices to manage threads with really huge computations?

    V 1 Reply Last reply 21 Jun 2016, 09:39
    0
    • J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 21 Jun 2016, 08:34 last edited by
      #2

      You could use a variable which is set (lets say to true) when you want to terminate the thread. The thread has to check that variable periodically and terminate if it is set.

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

      N 1 Reply Last reply 21 Jun 2016, 09:04
      0
      • J jsulm
        21 Jun 2016, 08:34

        You could use a variable which is set (lets say to true) when you want to terminate the thread. The thread has to check that variable periodically and terminate if it is set.

        N Offline
        N Offline
        Northsoft
        wrote on 21 Jun 2016, 09:04 last edited by
        #3

        @jsulm That's ok if nobody uses QThread::exit() method. Even if I make a slot for exit event, it could be unreachable because I don't launch event loop. Maybe there is a way to detect posted event?

        1 Reply Last reply
        0
        • N Northsoft
          21 Jun 2016, 08:27

          Hi all, I write a program which has a separate thread with computation routines about 1..10..30 minutes long. So event loop is useless here. But I'd like to keep thread manageable so I'm wondering how working slot should query thread instance about posted termination event. Are there any best practices to manage threads with really huge computations?

          V Offline
          V Offline
          VRonin
          wrote on 21 Jun 2016, 09:39 last edited by
          #4

          @Northsoft said:

          But I'd like to keep thread manageable so I'm wondering how working slot should query thread instance about posted termination event.

          If termination is the only thing you care I'd pass a const pointer to std::atomic_bool that the computation routine checks and terminates if false.

          If you can use C++11, for this kind of things where you don't need an event loop, I'd use the standard std::async instead as it might be more efficient and reduce the overhead of QThread

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 21 Jun 2016, 10:12 last edited by
            #5

            @Northsoft
            Hello,

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

            I don't think it's reasonable to use an event loop, without using an event loop (what you asked). You either use the event loop and batch your calculation work through that, or you don't and you handle the thread control by hand.

            Kind regards.

            Read and abide by the Qt Code of Conduct

            N 1 Reply Last reply 21 Jun 2016, 10:18
            2
            • K kshegunov
              21 Jun 2016, 10:12

              @Northsoft
              Hello,

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

              I don't think it's reasonable to use an event loop, without using an event loop (what you asked). You either use the event loop and batch your calculation work through that, or you don't and you handle the thread control by hand.

              Kind regards.

              N Offline
              N Offline
              Northsoft
              wrote on 21 Jun 2016, 10:18 last edited by
              #6

              @kshegunov thanks a lot!

              1 Reply Last reply
              0

              1/6

              21 Jun 2016, 08:27

              • Login

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