[Qt-interest] What exactly is thread affinity, and what is it for?
Thiago Macieira
thiago at kde.org
Thu May 20 17:05:18 CEST 2010
Em Quinta-feira 20 Maio 2010, às 16:27:39, K. Frank escreveu:
> Hello List -
>
> My assumption is that Qt's thread affinity is an association between
> a QObject (or derived class) and a specific instance of a QThread (or
> derived classes). (Hypothetically, QObject could contain a QThread*
> data member that points to the QThread for which it has affinity,
> although I don't claim that it is necessarily implemented this way.)
>
> Is this an accurate description of thread affinity?
Correct.
> What is thread affinity for? My understanding is that the main purpose
> (sole purpose?) of thread affinity is to determine whether a signal/slot
> connection is made through a Direct Connection or a Queued Connection:
> if the emitter (the instance of the class with the signal) and the receiver
> (the instance of the class with the slot) have the same thread affinity,
> the connection is direct, and if they have different thread affinities, the
> connection is queued.
The thread affinity is there to tell which thread's event dispatcher shall be
used for event-dispatcher activities.
The event dispatcher does basically three things:
1) it has an event queue
2) it has a list of timers
3) it has a list of socket notifiers
So when a QObject needs to handle any of those three things, it needs to know
which thread's event dispatcher it should talk to. The interface to the event
queue is QCoreApplication::postEvent; the interface to the timers is
QObject::startTimer (and the convenience classes QBasicTimer and QTimer); the
interface to socket notifiers is the QSocketNotifier class.
The signal/slot queued connection mechanism works by posting events. That's
why a queued connection is delivered in the thread that the object is affined
to.
The only extra operation is the determination of AutoConnection: if the target
object's thread affinity is the current thread, the signal is delivered directly
(decays to DirectConnection). If it's another thread, it's queued (decays to
QueuedConnection).
The emitter object's thread affinity has no influence in this process. It only
depends on the current thread, that is, the thread in which the signal is
emitted.
> Is this right? Is this the main use of thread affinity? Is thread
> affinity used for other things as well?
See above.
If you try to destroy a QSocketNotifier that has affinity to another thread or
stop a timer that belongs to another thread, you're going to get errors.
You're also going to get an error if you start a timer in an object that
belongs to another thread.
That means you must not touch the methods in any QObject that deal with timers
and socket notifiers.
> (These questions are motivated by an earlier post that tried to probe
> the behavior of some QThread and signal/slot example code.)
Note that QThread is a QObject class and it has no special-case support. Like
any QObject, it's thread affinity is to the thread in which it was created.
Unless you change it.
--
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/20100520/d51567b8/attachment.bin
More information about the Qt-interest-old
mailing list