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

Jérôme Godbout godboutj at amotus.ca
Fri Nov 30 20:15:40 CET 2018


The fact that QObject belong to Thread A and a method is used into Thread B should raise a flag, each QObject belong to a QThread and they should be used by that QThread only. You should move the Object to the other thread or use a signals / slots to communicate between thread.

You will have many problems the way you describe it, the fact that the thread B is launching the signal of Object that belong on Thread A, the automatic connection will check the ownership of the object emiting the signal to queue or not the signal and this will be wrong since the related thread is not the current thread.

Note: inheriting QThread is wrong practice and should probably never be done. Since the Qthread belong to the thread creator and not the created thread you will have the wrong current thread for the current QObject that inherit QThread.

You can see this article that explain the problem by inheriting the QThread: http://blog.debao.me/2013/08/how-to-use-qthread-in-the-right-way-part-1/

The Qthread doc and example should really be updated if they still show inheritance of QThread, many green developer fall into the pit every time and I can understand if the doc still do this.

-----Original Message-----
From: Interest <interest-bounces at lists.qt-project.org> On Behalf Of Alexander Dyagilev
Sent: November 30, 2018 1:53 PM
To: interest at qt-project.org
Subject: [Interest] Is it OK to emit from different thread?

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?

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