[Interest] Never ending thread: blocking GUI

Atlant Schmidt aschmidt at dekaresearch.com
Tue Oct 16 15:26:29 CEST 2012


  I think it's generally incorrect to do a QApplication::exec()
  from anywhere but your main thread (the GUI thread). Instead,
  your worker thread should just be waiting on a semaphore (set
  by the main thread) or some such. Note that no explicit "sleep()"
  is usually required.

  Inserting some debug messages can always be helpful, especially
  if those messages print the various thread IDs. On Linux, that's
  the value you get from:

      const pid_t ProcessID = getpid();                 // Get the Overall Process ID (which will match the main thread's TID)
      const pid_t ThreadID  = syscall( __NR_gettid );   // Get the Thread ID -- gettid() isn't in glibc so do it manually!


-----Original Message-----
From: interest-bounces+aschmidt=dekaresearch.com at qt-project.org [mailto:interest-bounces+aschmidt=dekaresearch.com at qt-project.org] On Behalf Of Sensei
Sent: Tuesday, October 16, 2012 9:15 AM
To: Qt Interest
Subject: [Interest] Never ending thread: blocking GUI

Hi! I am in the middle of a crisis, since I thought I understood
threads, but I might be wrong!

This is quite easy: I want to start a thread that never ends.

Once in a while, when the GUI needs it, it will "wake the thread up",
and run a method of my QThread subclass, in my case, a "find in files"
or "rename all files", or any other method.

I thought I could simply make run() be an infinite loop, sleeping for a
long period (one day).

I was wrong: I hang my GUI.

What are your recommendations? This is what I intended:

class ScanFiles : public QThread

     ScanFiles(QObject *parent = 0) : QThread(parent) { };

     // This should run endlessly, until the parent dies
     void run()
         qWarning("starting scanning thread.");

         // Where does this fit?

         while (1)
             qWarning("sleeping now...");
             // Snore for one day, now 5 secs just for debugging

public slots:

     // Here I will place the methods that should be run by the thread
     QString search(const QStringList &files);

Now, I think my question is quite easy, for someone who understands
QThreads... but I'm a complete newbie here :)

Thanks & Cheers!

Interest mailing list
Interest at qt-project.org

 Click https://www.mailcontrol.com/sr/fU1XXTDL7!!TndxI!oX7Uu4ItyQZZf3fH1aNphEiT6HKNBA75PnjouTi+lhxagYxjtsalfG7bb0jp+mmMV7JIw==  to report this email as spam.

This e-mail and the information, including any attachments, it contains are intended to be a confidential communication only to the person or entity to whom it is addressed and may contain information that is privileged. If the reader of this message is not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please immediately notify the sender and destroy the original message.

Thank you.

Please consider the environment before printing this email.

More information about the Interest mailing list