[Qt-interest] Temporarily 'ignoring' QIODevice::readyRead() in multithreaded app
Preet
preet.desai at gmail.com
Wed Dec 9 03:30:59 CET 2009
Hey all,
I'm having a very strange problem with this methodology. I have readyRead()
connected to a slot that simply prints out the number of bytesAvailable. For
some reason, after one round of I/O with my device, the bytesAvailable is
consistantly 0. Why would readyRead() even emit if this was the case?
On Mon, Dec 7, 2009 at 2:34 PM, Arnold Krille <arnold at arnoldarts.de> wrote:
> Hi,
>
> On Monday 07 December 2009 18:55:37 Preet wrote:
> > I didn't quite follow some of it. So if I call readAll() in my slot that
> is
> > connected to the readyRead() signal, it will automatically ignore
> > subsequent readyRead() signals?
>
> When you do readAll, it will read everything that is in the device and in
> QIODevices buffer at that time. When new data arrives while you process the
> old
> data, readyRead() is scheduled and if you do a next readAll() you will get
> that data.
>
> > > So when your slot isn't finished by the time the new signal arrives,
> that
> > is
> > > not a problem per se as the next signal-slot will only execute
> > > afterwards.
> > I didn't get what you meant by 'execute afterwards'...
> > Within the same thread is there anyway I can ensure that a slot that is
> > signaled runs to completion?
>
> Within one thread there is only one event-queue and only one way to execute
> code. So the slot connected to readyRead() will always only be executed
> once
> at a time. And if your device lives in a different thread, the signal-slot
> connection crosses thread-boundaries resulting by default in a queued
> connection. Which is actually an event that is appended to the receiving
> object event-queue. Again your receiving slot will only be called once at a
> time.
>
> Basically what I have in mind is the following:
>
> void on_readyRead() {
> while( device->bytesAvailable() > 0 ) {
> // do your heavy work involving readAll to get all data currently present
> }
> }
>
> So when data arrives, this function is called. When more data arrives while
> the function is running, it will process that data too. But the additional
> calls because of the additional readyRead-signals will find an empty device
> and
> thus do nothing.
>
> Hope that helps,
>
> Arnold
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091208/5f5ca19f/attachment.html
More information about the Qt-interest-old
mailing list