[Interest] Is it OK to emit from different thread?

Marian Beermann public at enkore.de
Fri Nov 30 20:35:21 CET 2018


Note that the default is "AutoConnection", which...

> (Default) If the receiver lives in the thread that emits the signal,
Qt::DirectConnection is used. Otherwise, Qt::QueuedConnection is used.
The connection type is determined when the signal is emitted.

Cheers, Marian

Am 30.11.18 um 20:14 schrieb Konstantin Tokarev:
> 
> 
> 30.11.2018, 22:04, "Alexander Dyagilev" <alervdvcw at gmail.com>:
>> Hello,
>>
>> Let's suppose some QObject derived class belongs to thread A. It has
>> some method, which emits some signal.
>>
>> This method may be called from another thread B. Thus, signal will be
>> issued for this object from the thread it does not belong to.
>>
>> Is it OK?
> 
> Given that "emit" is no-op keyword and emission of signal is just a method call,
> it's fine to do this. However, exact behavior will depend on how particular slots
> are connected to that signal: slots with direct connection will run in thread B,
> not A.
> 
>>
>> source code (just in case):
>>
>> class MyObject {
>> Q_OBJECT
>> signals:
>> void mySignal();
>> public:
>> void test();
>> }
>>
>> void MyObject::test()
>> {
>>       emit mySignal();
>> }
>>
>> // thread A:
>> ...
>> auto obj = new MyObject();
>> ...
>>
>> // thread B:
>> ...
>> obj->test();
>> ...
>>
>> _______________________________________________
>> Interest mailing list
>> Interest at lists.qt-project.org
>> https://lists.qt-project.org/listinfo/interest
> 




More information about the Interest mailing list