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. QThread and loop
Qt 6.11 is out! See what's new in the release blog

QThread and loop

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.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.
  • T Offline
    T Offline
    terryfryar
    wrote on last edited by
    #1

    Using a QThread and a worker (not subclassed from QThread, but subclassed from QObject and is using thread event loop with signal/slots) I have a "permanent" loop to execute in a thread. In the old days of pthread, you might have:

    void mythread() {while (!terminated) {do stuff......}}

    Where terminated might be a global var, or in a non-global scope somewhere. Now, for Qt...how to tell if the thread is terminated by an outside thread or the main? I want the worker function to terminate nicely!

    The worker threaded function will be moved to the QThread's thread. So, for best practice should I:

    • use QThread's "isRunning/isFinished"?
    • check the actual thread state?
    • use signal/slot to trigger a state variable private to my worker?

    Also, if I use a signal/slot to fire off the "eternal" function (ie it doesn't return unless thread terminated)...does this block the thread's event loop?? Thanks!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      Even in a thread, the thread's eventloop needs to be allowed to run. If your thread code is in a tight loop (without a call to processEvents() at a very minimum,) then your loop will block the thread's eventloop.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      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