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
Forum Updated to NodeBB v4.3 + New Features

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 1 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.
  • NorthsoftN Offline
    NorthsoftN Offline
    Northsoft
    wrote on 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?

    VRoninV 1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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

      NorthsoftN 1 Reply Last reply
      0
      • jsulmJ jsulm

        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.

        NorthsoftN Offline
        NorthsoftN Offline
        Northsoft
        wrote on 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
        • NorthsoftN Northsoft

          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?

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on 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
          • kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on 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

            NorthsoftN 1 Reply Last reply
            2
            • kshegunovK kshegunov

              @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.

              NorthsoftN Offline
              NorthsoftN Offline
              Northsoft
              wrote on last edited by
              #6

              @kshegunov thanks a lot!

              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