PDA

View Full Version : Speed up Producer Consumer Threads



^NyAw^
29th February 2008, 15:34
Hi,

I have two threads in my application. One is a Producer and the other one is a Consumer. The proucer does some work and create a own class object that is inserted on a ring buffer. The consumer reads this buffer objects(when there are almost one).
The consumer then emits the consumed object to the main thread to let it display some data of this object, but it is done by value(not by reference or pointer). This is done because if I pass the object by reference or pointer to the main thread, maybe when it goes to read it, the consumer thread is modifying it and then the values are incorrect.

I want to speed up my application and my question is to use a QMutex ring buffer wich will lock the object so I could pass it as reference(without spending the time to copy the object), and when the main thread finish the reading process it will unlock it to let the consumer and producer continue.

Have anyone a good idea how to speed up this process?

Thanks,

wysota
29th February 2008, 15:38
Hmm... mutexes slow down applications, not speed them up...

^NyAw^
29th February 2008, 15:44
Hi,



Hmm... mutexes slow down applications, not speed them up...


This is another question... I use a ring buffer with QWaitConditions to lock the threads but I was thinking on use a QQueue without mutexes or waitConditions, so the producer queues objects and the consumer dequeues objects. But the problem will be if the producer is faster than the consumer.

Thanks,

wysota
29th February 2008, 15:47
wait conditions use mutexes so... you are exchanging mutexes for mutexes... I suggest you use custom events to transfer data to the main thread and let some object there handle them. This way you won't be copying whole objects, just the data you really need. Custom events are thread safe, so you won't need any additional synchronisation there.

^NyAw^
29th February 2008, 15:51
Hi,

I have not readed about custom events, is there a big difference to use signals and slots?

wysota
29th February 2008, 16:29
Actually signals across threads are delivered using events, so you may use signals if you want. The whole point is to transfer only the data, not the whole object. If you really have to transfer the whole object, make it implicitly shared, it will then be very lightweight. Read about QSharedData and QSharedDataPointer to learn how to implement implicitly shared classes.

^NyAw^
29th February 2008, 17:27
Hi,

I have forget to tell that the objects that I'm passing thorugh signals and slots are from closed source lib and I have not access to them. So maybe creating my own class derived from QSharedData and inserting a object reference(from the closed source lib) into it will be possible.
Then, this object could be shared. Right?

Thanks,

wysota
29th February 2008, 18:38
No, there is no point in doing that and it would not work anyway. In that case I suggest you extract all data you need from the object, send the data to the other thread and forget about the object. Another possibility is to consume in the GUI thread - then you'll be able to draw immediately.