[Qt-interest] Temporarily 'ignoring' QIODevice::readyRead() in multithreaded app

Arnold Krille arnold at arnoldarts.de
Mon Dec 7 20:34:00 CET 2009


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
-------------- 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/20091207/7d754d4d/attachment.bin 


More information about the Qt-interest-old mailing list