[Interest] QThreads: There's no one way to "do it right"! :)

Bo Thorsen bo at fioniasoftware.dk
Wed Jan 23 13:08:23 CET 2013


Den 23-01-2013 08:42, Mandeep Sandhu skrev:
> First I thought "I was doing it right"
>
> Then they said "you're doing it wrong"
> (http://blog.qt.digia.com/blog/2010/06/17/youre-doing-it-wrong/)
>
> Then I thought "I _finally_ got it right"!!
>
> Now they say "You were not doing so wrong"
> (http://woboq.com/blog/qthread-you-were-not-doing-so-wrong.html)!! :D
>
> End of my Cute poem! :P
>
> Jokes apart, which method do most ppl here prefer when doing threading?

Your question is wrong. Here's why:

I have three ways to use QThread (QtConcurrent not included):

1) Worker thread that uses moveToThread approach
2) Subclass QThread do stuff in run() - no event loop involved
3) Subclass QThread and initialize a bunch of objects in run() followed 
by a call to exec()

I'll use any one of them based on what I want to achieve. The third is 
the one that can get the YOU ARE DOING IT WRONG bunch upset. The 
argument against it is that it's probably the most difficult to get 
right, unless you know what you are doing.

There are problems in each approach to using QThread, though. And if a 
developer doesn't understand the issues involved, he will probably end 
up doing something evil - like moveToThread(this).

The reason that #1 is the most popular is that it's the one where 
developers are less likely to shoot themselves in the foot. Which is 
true. However, if you're on a project with me, there's no way you are 
allowed to use anything as big and powerful as QThread, if you don't 
understand the issues involved with the choices. As the Oracle so 
perfectly states it: "You can never see past the choices you don't 
understand." So it's a somewhat moot point, because I believe strongly 
that anyone using QThread needs to fully understand how objects live in 
the threads and how signals are propagated to slots between objects when 
threads are involved. And when you really understand this, all choices 
are available to you.

So if someone just says he prefers one approach over all the others, 
it's because he doesn't fully understand the pros and cons for the 
choices. And that's why your question is wrong. There is no right 
choice. Which fortunately for you is exactly what you had in the mail 
subject :) However, if someone answers that he thinks #1 is more often 
appropriate for the task at hand, then I won't argue against it.

I have answered this before on the mailing list, and I probably will again.

A final note. I have three rules about threading:

1) Don't do it.
2) Don't do it.
3) Don't do it yet.

If someone on this list is considering threads in their projects, he 
should educate himself to the point where he understands the choices and 
why those three rules makes sense. Then and only then, can he consider 
himself allowed to wield the big hammer :)

Bo Thorsen.

-- 
Fionia Software - Qt experts for hire.




More information about the Interest mailing list