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

Thiago Macieira thiago at kde.org
Tue Jan 5 17:06:43 CET 2010


Em Terça-feira 05 Janeiro 2010, às 11:58:37, Alexander Kurz escreveu:
> Hi All,
> 
> 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...
> 
> 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.

You don't need anything new. Use what you knew from before.

Instead of placing your data in the signal arguments, place it in a shared 
memory region, protected by a mutex, maybe a ring buffer like Arnold suggested.

Your slot is woken up by the signal and then extracts the data from the shared 
container.

But in fact I wouldn't even use signals and slots. QWaitCondition is perfect 
for your use, or QSemaphore.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100105/45733427/attachment.bin 


More information about the Qt-interest-old mailing list