[Qt-interest] cross thread signals slots and event queue
Alexander Kurz
lewi.alex at gmail.com
Wed Jan 6 09:50:55 CET 2010
Hi All,
Thank you very much for your ideas.
I have implemented my problem currently similar to Arnolds approach.
Thread 1 is sending the signal in direct connection (callback). In the slot
implementation the parameters are stored to shared memory protected by
mutexes und the calculation thread is notified by waitcondition.
Thread 2 now implements the run() method where it waits for the
waitcondition to be set inside a loop. If the waitcondition comes true it
retreives the data from shared memory and starts calculation with this data.
Now I have the loose coupling of the 2 threads with signals/slots, combinied
with the fact that only latest data is used for calculation if it happens
that calculation takes longer than new data arrives.
Cheers!
On Tue, Jan 5, 2010 at 5:06 PM, Thiago Macieira <thiago at kde.org> wrote:
> 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 --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100106/9d209690/attachment.html
More information about the Qt-interest-old
mailing list