[Qt-interest] Can two thread share a QAbstractSocket?
Thiago Macieira
thiago at kde.org
Wed May 12 23:54:30 CEST 2010
Em Quarta-feira 12. Maio 2010, às 21.59.20, Gabriele Kahlout escreveu:
> > And where's the need for thread here? Sure, it takes time to transfer.
> > But so
> > what?
>
> Fairness /concurrency. It's undesirable to have the 2nd client wait until
> those who came first are completely served.
So don't introduce the waiting. I don't see why you would.
You don't need threads to solve that particular problem. The example I gave
you, minus the threading, already does it.
> > I don't think you understand UDP. Please study it before thinking that it
> > serves your purpose.
>
> So the primary relevant issue here is that it's connectionless, but faster
> (& unreliable). However as quoted above AbstractSocket should abstract
> enough. Instead of listen bind (QUdpSocket::DontShareAddress ).
> I want to transfer images as fast as possible. I don't think desktop
> sharing (for instance) would use TCP.
>
> The trouble is with virtual connection. How do I 'imitate' this with
> QAbstractSocket/UDP?
By virtual connection, do you mean UDP's socket ability to send and receive
only to/from a specific host? connectToHost() will do that.
If you mean ensuring the retransmission of lost packets, you'll have to write
your own protocol on top of UDP.
> //while(waitForReadyRead(-1)){ //I prefer blocking. "A blocking socket
> doesn't require an event loop and typically leads to simpler code".
> and then??
Indeed, but it also requires threading in order to avoid blocking. So the
moment that you're trying to handle more than one client, you move from
"simpler code" to "more complex code".
In my experience, if you're trying to handle more than one socket,
asynchronous, non-blocking code leads to simpler code overall due to the
absence of threads, even if localised it looks more complex.
> new ClientHandler(serverSocket.localAddress(), serverSocket.localPort())
> (used by the clientHandler to create a new socket? )
Didn't understand.
> > You can change it to DirectConnection. I'm using a separate object here
> > to make it perfectly clear to you: this is the most common error people
> > make when
> > using threads along with signals and slots.
> >
> > > //or waitForReadyRead().
> >
> > That's blocking, and you won't need the slot.
>
> blocking + threading, gui + slots.
Slots aren't inherent to GUI. But in the GUI thread, you want asynchronous,
non-blocking code.
> > But the code I designed, I did it so that it won't need threads.
>
> How come what about clientHandler.start()?
I said it doesn't need threading. But I was answering your question on how to
use threads. You can remove the threading code I added and it will work.
> > > > Note that the threading bits in my code are completely unnecessary.
> > >
> > > // u mean the SomeOtherObject stuff?
> >
> > No, I mean the threading. The SomeOtherObject is necessary to receive the
> > readyRead() signal.
>
> To achieve concurrency you still need clientHandler.
No, you don't. You need the SomeOtherObject in my example. The thread is
unnecessary. And concurrency is NOT sacrificed. Many different sockets can be
handled, simultaneously, without blocking, from the same thread. And with
overall simpler code.
> > (Hint: get a mail reader that strips the signatures off)
>
> My client (gmail web interface) collapses all the 'footer' stuff of emails
> I reply into "- Zitierten Text anzeigen -".
Good. Now make sure it doesn't include that into the reply. All signatures are
preceded by dash-dash-space, so they are quite easy to detect and remove.
--
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/20100512/f01918ab/attachment.bin
More information about the Qt-interest-old
mailing list