[Qt-interest] Socket communication without threads

Arnold Krille arnold at arnoldarts.de
Mon Mar 2 17:21:08 CET 2009


On Monday 02 March 2009 17:08:37 Predrag Manojlovic wrote:
> 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:
> >> 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.
> Looks like only reason is concurrent access.
> I was afraid that I missed something important in my experience :)

"Only concurrent access" _is_ something important. Its actually a lot harder 
than it seems, at first beginners start with no locking at all giving rise to 
all kinds of strange errors that are hard to debug (and when you think you 
mastered them they come back when you run your app on a multi processor 
machine). Then normally the second thing people do is all a lot of hard 
locking where ever data is accessed from multiple threads which mostly results 
in only one thread being active at a time (which is the same as just using the 
main thread). And then you start reading up on the matter and think about it 
and either realize that you don't need multiple threads at all or that you 
have to discriminate between places where hard locking as in "using QMutex" or 
softer locking as in "using QReadWriteLock" or "using QSemaphore" is needed. 
And after that its still a lot of trouble and hard debugging...

So when people start with Qt and want to write "network servers" it is mostly 
right to not use threads at first.

Have fun,

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/20090302/b3a9555f/attachment.bin 


More information about the Qt-interest-old mailing list