[Qt-interest] cross thread signals slots and event queue

Arnold Krille arnold at arnoldarts.de
Tue Jan 5 15:54:29 CET 2010


Hi,

On Tuesday 05 January 2010 11:58:37 Alexander Kurz wrote:
> I would like to use signals and slots with queued connection between 2
> threads.
> 
> Thread A listens on a serial channel and receives data let's say every 1
> sec.
> Thread B does some calculation with the received data.
> 
> When the calculation takes now longer than 1 second the queue where the
> signals are queued gets bigger and bigger until I run out of memory...

You would have to add your own counter that is set to (say) 5000 at start, 
decremented by thread A, incremented by thread B and A only sends events (and 
decrements the counter) when there are numbers left (the counter is greater 
than zero). That is called a semaphore :-)

> Before Qt I used shared memory, mutexes, waitconditions for
>  synchronisation. Like you would implement a consumer/producer pattern but
>  with only 1 shared data memory and no queue. There it was no problem
>  because it always used the latest data for calculation and no queue got
>  expanded.
> I would really like to use signals and slots for this usecase because it
> would makes things much easier and less code.
> I need a mechanism to limit the size of the event queue of a specific
>  thread or to queue only signals that are not queued already. I could not
>  find anything like this in the documentation, so maybe someone here could
>  give me a hint.

I would make the receiving slot not do the calculations but instead put the 
data into a buffer. Might be a ringbuffer. That way only the receiver controls 
the size of the queue. And the receiver would be a QThread and inside its 
run()-method it checks if there is data to process and otherwise sleeps for 
some time. While the slot would be executed in the thread the receiver is 
living in, the run-method is running in its own thread. Then it doesn't really 
matter if sender and receiver live in the same thread or not, the calculation 
is done in its own thread all the time.

Maybe that helps,

Arnold
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100105/898c79ea/attachment.bin 


More information about the Qt-interest-old mailing list