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

Konstantin Tokarev annulen at yandex.ru
Fri Nov 30 20:14:23 CET 2018



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

-- 
Regards,
Konstantin




More information about the Interest mailing list