November 25, 2010

iunknwn iunknwn
Lab Rat
34 posts

Discussion regarding QWaitCondition. When to use?

 

  1. #ifndef THREADSAFELIST_H
  2. #define THREADSAFELIST_H
  3.  
  4. #include <QtCore/QReadWriteLock>
  5. #include <QtCore/QReadLocker>
  6. #include <QtCore/QWriteLocker>
  7. #include <QtCore/QList>
  8.  
  9. template<typename T>
  10. class ThreadSafeList
  11. {
  12. public:
  13.  explicit ThreadSafeList() {}
  14.  
  15.  int count() const
  16.  {
  17.   QReadLocker locker(&lock);
  18.   return list.count();
  19.  }
  20.  
  21.  const T& at(int i) const
  22.  {
  23.   QReadLocker locker(&lock);
  24.   return list.at(i);
  25.  }
  26.  
  27.  
  28.  void append( const T& value )
  29.  {
  30.   QWriteLocker locker(&lock);
  31.   list.append(value);
  32.  }
  33.  
  34.  
  35. private:
  36.  mutable QReadWriteLock lock;
  37.  QList<T> list;
  38. };
  39.  
  40. #endif // THREADSAFELIST_H
  41. //EOF

Having above code, I’ve GUI thread that creates this list on a heap and passes the pointer to another thread.

Now as the program progresses, GUI thread pushes in new elements in the list by calling “append” and then emits the signal with new tail index. This signal is connected to the reader object with thread-affinity to another thread which by using the “at” method reads the value till it reaches the tail.

So GUI thread is producer and the other thread is consumer.
GUI thread produces data as it gets from its source and doesn’t care what this other thread has consumed.
Consumer thread only consumes till it reaches the tail. Then it just sits idle till it gets signal of new tail from producer.

The threads are introduced so that GUI is free to do other things while thread consumes this data.

Having said all above. Do I need to have wait condition?

Am I locking data or code from above example?

 Signature 

Vista x64 with Qt 4.8.2 and VS2010

3 replies

November 25, 2010

Gerolf Gerolf
Area 51 Engineer
3210 posts

Hi,

the above example locks data, thats ok. I would stay with this. A wait condition is needed, if you don’t communicate via signals / slots.
If you create a worker thread that ahs, for example a stack of orders to work on. If the stack is empty, it should wait for the next order. Then you can use a wait condition and in the push orter method, you would wake up the thread via the wait condition. Both ways work. It’s just a decission of what looks better for you :-)

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

November 26, 2010

iunknwn iunknwn
Lab Rat
34 posts

Thanks Gerolf for the reply.

I’m sure after spending whole bunch of hours, coding, building, testing and debugging would’ve yielded me the same affirmative answer as I got from you, but it is this type of assurance and time-savings that one get in forums like this before getting on the attack-plan.

I think I can get away without using QWaitCondition.

 Signature 

Vista x64 with Qt 4.8.2 and VS2010

November 27, 2010

dfaure dfaure
Lab Rat
41 posts

Yes, the wait condition would be for a “blocking” thread, while signal/slots are for a “thread with an event loop”.

 Signature 

David Faure (.(JavaScript must be enabled to view this email address))
KDE/Qt Senior Software Engineer
KDAB - Qt Experts - Platform-independent software solutions

 
  ‹‹ Invalid parameter passed to C runtime function.      activateWindow() does not send window to front ››

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