[Qt-interest] Socket communication without threads

Scott Aron Bloom Scott.Bloom at sabgroup.com
Mon Mar 2 17:24:12 CET 2009


> Andreas Pakulat wrote:
> > On 02.03.09 16:36:27, Predrag Manojlovic wrote:
> >> Often i hear that threads should be avoided in qt socket
applications.
> >> I'm not sure is that possible always.
> >
> > Of course its not _always_ possible, but most of the time it is.
> >
> >> I'm mostly creating applications like TCP chat client/server.
> >>
> >> Questions:
> >> 1. Is there any way to create chat server without using threads?
> >
> > Can't answer that one.
> >
> >> 2. why is that better?
> >
> > In general using only 1 thread is better because you avoid all
> > concurrent-access problems you get when having multiple threads. You
don't
> > need to make sure you're accessing data only from one thread at a
time,
> > debugging is a lot easier, you avoid a lot of possible timing issues
> > because sometimes one thread runs "faster" than another and causes a
> > problem.
> >
> > Andreas
> >
> 
> Looks like only reason is concurrent access.
> I was afraid that I missed something important in my experience :)
> 
> That answers to my questions :)
> Thank you.

One of the other reasons... In QT, much of the code is a wrapper code
around OS/platform specific code, where we as users of the framework
only have access to the platform independent interface..

The downside to this, is we often are kept in the dark, coding with a
huge smile, but not realizing how much of the system code we access is
accessed and implemnented.  

In this case, the asynchronous socket code, already uses threads, we
however don't need to worry about it. TrollTech took care of it for us,
we just connect to the signal, like stateChanged or readyRead etc etc...

Now QT, gives us blocking methods of using the socket code
(waitForReadyRead for example), but all they tend to do, is implement a
loop around the thread waiting for a signal or a timer to timeout.

So you explicitly using a QTHread around the "forced blocking calls"
usually is not necessary...

I have used the QTHread wrapper around the blocking in the past, but its
usually not necessary.

Scott




More information about the Qt-interest-old mailing list