Multithreading – issue a QT signal from a non QT thread or issue a QT main event loop at 4.5

I call an emit signal1 () from a non QT thread

It is just a pthread (pthread_create()), which calls a signaled QObject method

For example:

MyQbject: public QObject
{
...
void emitBunchOfSignals()
{
 emit signal1();
 emit signal2();
 ...
}
...
}

The "run" method of my pthread has a pointer to the MyObject instance (the instance created in the context of the main QT GUI thread, not pthread) and calls the emitbunchofsignals() method

It was annoying before QT 4.5 Now, does QT 4.5 handle this? Does it call QAPP - > postevent() or something so that the signal is emitted in the QT GUI thread (and therefore the slot)?

thank you

Solution

You need to make sure to use queued connections with threads, because QT cannot automatically identify the object belonging to which thread ("thread affinity" is a term used in the document) When connecting, do the following:

connect(src,SIGNAL(signal-signature),dest,SLOT(slot-signature),Qt::QueuedConnection);

This will cause the signal to be placed in the event loop of the destination and called when its thread is running (i.e. its event loop)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>