April 14, 2012

rain_spider_80 rain_spider_..
Lab Rat
4 posts

Persistent threading with Qt

 

I need to make some persistent threads. By this I mean that I would like them to live for the duration of the application. For the sake of brevity, lets pretend that I need two worker threads. The threads will be fed continuously throughout the life of the application. I need to know in an efficient way when the two threads are both finished the current work assigned to them, so that I can assign them new work.

At the following link [doc.qt.nokia.com] it is suggested that if one wants persistent threads (that don’t die when their run function completes), that we should do threading as follows:

  1. class Worker : public QObject
  2. {
  3.     Q_OBJECT
  4.  
  5. public slots:
  6.     void doWork() {
  7.         /* ... */
  8.     }
  9. };
  10.  
  11. QThread *threadA = new QThread;
  12. QThread *threadB = new QThread;
  13. Worker *workerA = new Worker;
  14. Worker *workerB = new Worker;
  15. workerA->moveToThread(threadA);
  16. workerB->moveToThread(threadB);
  17. threadA->start();
  18. threadB->start();
  19. QMetaObject::invokeMethod(workerA, "doWork", Qt::QueuedConnection);
  20. QMetaObject::invokeMethod(workerB, "doWork", Qt::QueuedConnection);

My problem with this approach is that we cannot use the QThread::wait() function because that only works when you use the scheme where you implement run(). This leaves me with no way to know when the threads have finished their work. Instead this only tells me when the threads have been exited. If I use some flags that the threads manipulate, then I’m left in a situation where I have to poll constantly checking those flags, which steals cpu time from the workers. We need a mechanism where the caller that feeds the threads waits until the threads are ready to be fed more work, but no longer. If there was a way to use signals to reflect thread work completion I would still be in trouble because that still leaves the caller needing to be suspended and then woken up, but signals cannot wake up the caller which initiated everything to begin with. As far as I know, a signal can only call a function. It cannot unfreeze an already executing function that is waiting for some event.

The docs state that if we want to not kill the threads when they have finished some work that they can be kept in a pool and wait to be used again later. Code sample below:

  1. class Work : public QRunnable
  2.  {
  3.  public:
  4.      void run()
  5.      {
  6.          qDebug() << "Hello from thread " << QThread::currentThread();
  7.      }
  8.  };
  9.  
  10.  int main(int argc, char *argv[])
  11.  {
  12.      QCoreApplication app(argc, argv);
  13.      Work workA;
  14.      Work workB;
  15.      workA.setAutoDelete(false);
  16.      workB.setAutoDelete(false);
  17.      QThreadPool *threadPool = QThreadPool::globalInstance();
  18.  
  19.      //first chunk of work
  20.      threadPool->start(&workA);
  21.      threadPool->start(&workB);
  22.      threadPool->waitForDone();   //doesn't this remove the threads from the thread pool?
  23.  
  24.      //second chunk of work
  25.      threadPool->start(&workA);
  26.      threadPool->start(&workB);
  27.      threadPool->waitForDone();
  28.      return 0;
  29.  }

The docs contradict this statement in the same document because they categorize the QThreadpool implementation as a “one call” thread life scheme. Also the docs say that when you call waitForDone(), that “Waits for each thread to exit and removes all threads from the thread pool.” If the threads are removed from the thread pool then obviously this thread pool scheme cannot be used to recycle threads.

Any clarifications would be greatly appreciated.

7 replies

April 14, 2012

1+1=2 1+1=2
Hobby Entomologist
638 posts

I am confused ;-)

Your first sentence is:

I need to make some persistent threads.

So, IMO, Method1 should work for you.

But, then, you complain that

My problem with this approach is that we cannot use the QThread::wait() function because that only works when you use the scheme where you implement run(). This leaves me with no way to know when the threads have finished.

1. Why you want “persistent threads” finished?
2. QThread::wait() should works well.
3. If you want to exit one thread,

  1. thread->exit();
  2. thread->wait();

4. If you want to get a signals just when your doWork() finished, you should add a signal to you class, then emit it before doWork() finished.

April 14, 2012

rain_spider_80 rain_spider_..
Lab Rat
4 posts

1. You trimmed my quote. I didn’t say finished… I said finished their work. By finished I don’t mean I want to know when the threads have quit() or exit(), instead I need to know when the persistent threads have finished doing the work that I assigned to them(when the function I told them to work on has finished its work) so that I can assign more work to the threads. That is the whole point of persistent threads… instead of terminating them… you keep feeding them with more work.
2. Maybe QThread:wait() “should” work but it doesn’t! When you implement run() then QThread::wait() returns on 3 conditions. One, run is done. Two, thread has quit() or exit(). Three, timer runs out. When you do threading as shown above then wait() only works with conditions two and three. Thus, there is no way to know when run() has finished its work because there is no run(). As such, in such a scenario QThread::wait() only tells us when the thread has terminated and since we are dealing with persistent threads this is useless info because that will be at the end of program.
3. I don’t want to exit threads. I want to keep the threads alive and keep feeding them more work.
4. Signals don’t work for the scenario described in the original post.

  1. //Imagine an outside force like an OS/driver using using a callback function.
  2. infinite loop by external force
  3. {
  4.    //Gets called a lot so want to re-use threads instead of creating new threads each time.
  5.    C callback function(some parameters)  
  6.    {  
  7.        //Have threads help with processing work in this function that is computational expensive.
  8.        Gives some existing pre-made threads some work to do and wait for threads to finish
  9.         work (do not terminate threads   because they will be re-used next time C callback function is called).
  10.  
  11.         Do some more work in here after current computation by threads are complete.  
  12.     }
  13. }

So, the callback is like a driver. It gets called by outside user I can’t influence. Need some way to pause execution of C callback function while threads finish work. Don’t confuse this with threads finishing in the sense that they are terminated. As soon as the threads finish work, the call back should resume with its remaining work. Then…when the callback is called again I want to feed the same existing threads with new work. I don’t want to create new threads in the callback function because creating threads is expensive. Threads are made at start of app and terminated at end of app. I don’t see how a signal will solve the issue at all. A signal can only call some function. That is not what I need. I need a way to pause execution of a running function (the callback function) and to resume execution of the function when threads have done their work.

The main problem is that there is no way to block in the callback function until threads are done working. QThread::wait() when working with persistent threads as shown in original post only unblocks when thread die or timer runs out. This is useless. I could run a busy loop checking some flags but that is not efficient and steals CPU time from worker threads.

April 14, 2012

RaubTieR RaubTieR
Lab Rat
42 posts

From QThreadPool docs i see:
Threads that are unused for expiryTimeout milliseconds are considered to have expired and will exit. Such threads will be restarted as needed. The default expiryTimeout is 30000 milliseconds (30 seconds). If expiryTimeout is negative, newly created threads will not expire, e.g., they will not exit until the thread pool is destroyed.

Which means you can really make them persistent as you desire. Quote sorce [qt-project.org]

April 14, 2012

1+1=2 1+1=2
Hobby Entomologist
638 posts

1. You seems mis-understand the function of QThread::run(), QThead::wait(), QThread::exec(), QThread::quit(), QThread::exit(), …

2. quit(), exit() only works when a eventloop is running in the thread. If you re-implement the QThread::run(), and QThread::exec() is not called in this function, quit() and exit() do nothing.

3. Once QThread::run() finished, the Thread finished! QThead::wait() only returned when the thread finished.

4. As I have said before, if you don’t want to exit the thread, an want to using signal and slots, Method1 is designed for you.

April 15, 2012

rain_spider_80 rain_spider_..
Lab Rat
4 posts

At to point number 4, the problem with Method 1 is that there is no efficient way to block the caller until all the threads have completed a currently assigned chunk(round) of work. I’ll highlight this sample of pseudo code again to illustrate the problem.

  1.     //Imagine an outside force like an OS/driver using using a callback function.
  2.     infinite loop by external force
  3.     {
  4.        //Gets called a lot so want to re-use threads instead of creating new threads each time.
  5.        C callback function(some parameters)  
  6.        {  
  7.            //STEP 1
  8.            //Feed 2 threads with computational expensive work.
  9.            //threads should be pre-made threads to save time on thread creation.
  10.            //do not terminate threads because they will be re-used next time C callback
  11.            //function
  12.            //is called with a new round of work.
  13.  
  14.             //STEP 2
  15.             //Need to pause/wait/BLOCK function execution here until the 2 worker threads
  16.             //have completed the work.
  17.             //THIS IS THE PROBLEM! No efficient way to block! I don't see how a signal/slot
  18.             //can be used for this purpose.
  19.  
  20.             //STEP3  
  21.             //After 2 threads complete their assigned work, resume execution of this C
  22.             //callback function.  
  23.             //Perform some non-threaded work in this function to complete the job.
  24.  
  25.             //return (end function execution), all work for this round/call is complete.
  26.         }
  27.     }

The problem with Method 1 is that there is no way to block as explained in the above pseudo code. If you know of an efficient way, please show me how. I think if you think through the signal slot thing you will see that they can’t be used for blocking. This leads back to what I started with in this post, there doesn’t seem to be a efficient way in Qt to know when persistent threads have completed a round of work. I can only know this by checking some flags in a while loop repeatedly which steels CPU time away from the threads.

As for Method 2, using a QThreadPool, if as in the code sample at the top of the post, if threadPool->waitForDone(); is used for BLOCKING, the docs say that
waitForDone() removes all threads from the thread pool!
That means…provided that the docs are not in error, that threadPool->waitForDone() can no be used if the goal is a persistent threading scheme. Leaving me with once again no efficient way to BLOCK until the threads have completed a round of work.

April 15, 2012

1+1=2 1+1=2
Hobby Entomologist
638 posts

Several questions:

1. Why there this a infinite loop´╝č In which thread it is running? Does this thread have a eventloop?

  1.     //Imagine an outside force like an OS/driver using using a callback function.
  2.     infinite loop by external force
  3.     {

If this thread does not have a eventloop, it can not a UI thread, you can using QMutex, QWaitCondition, ….

If this thread does have a eventloop, this inifinite can be removed, signals and slots will work across threads.

2. ThreadPool is designed for this.

  1.            //STEP 1
  2.            //Feed 2 threads with computational expensive work.
  3.            //threads should be pre-made threads to save time on thread creation.
  4.            //do not terminate threads because they will be re-used next time C callback

Again, you should use QMutex, QWaitCondition, ….
Though they works with ThreadPool, but ThreadPool and Runable does not designed for this case.

  1.             //STEP 2
  2.             //Need to pause/wait/BLOCK function execution here until the 2 worker threads
  3.             //have completed the work.

3. Yes, this is true. you want the threadPool finished. and when the pool finished, it will delete all the QThreads which are managed by the pool.

  1. As for Method 2, using a QThreadPool, if as in the code sample at the top of the post, if threadPool->waitForDone(); is used for BLOCKING, the docs say that
  2. waitForDone() removes all threads from the thread pool!

April 16, 2012

rain_spider_80 rain_spider_..
Lab Rat
4 posts

1. The operating system is calling my c function. Very often.

3. I DO NOT want the thread pool to delete any threads when the work is finished.

I don’t see how QMutex or QWaitCondition can be used for blocking until threads have completed the current work assigned to them for the current C_Callback)function call. if you think this can be done, I’d like to see some code to demonstrate this. The devil is in the details…

 
  ‹‹ integrating Qt with visual c++ 2008      is the QStandarditemModel - finditems() fast? ››

You must log in to post a reply. Not a member yet? Register here!