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

Jérôme Godbout godboutj at amotus.ca
Fri Nov 30 21:29:26 CET 2018


>> No, I will not in this particular case :)
Might be, but I hope nobody implement a QThread inherited class slots or signals into the mix.

As for the runtime check with the queued connection, take care, this is how it's checked:
QObject * const receiver = c->receiver;
 const bool receiverInSameThread = QThread::currentThreadId() == receiver->d_func()->threadData->threadId;

if ((c->connectionType == Qt::AutoConnection && !receiverInSameThread)
...

Take care of QThread thread Id might surprise you. I strongly recommend you print the thread id to make sure your use case can and will do what you expect. Only the current thread and the receiver are used to determine the automatic connection queued or not.


-----Original Message-----
From: Alexander Dyagilev <alervdvcw at gmail.com> 
Sent: November 30, 2018 2:47 PM
To: Jérôme Godbout <godboutj at amotus.ca>; interest at qt-project.org
Subject: Re: [Interest] Is it OK to emit from different thread?

On 11/30/2018 10:15 PM, Jérôme Godbout wrote:
> 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.

Well, I always do so. But this object is a special case. It's not used by other threads. It uses threads to perform various asynchronous tasks using lambdas, called from these threads, and these lambdas are supposed to return some result to it.

> You will have many problems the way you describe it,

No, I will not in this particular case :)

> 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.

Hmm... I.e. if both A and B objects belongs to the same thread and slot of A is connected to signal of B, then if signal of B is issued from another thread, then slot of A will be called in this different thread, not the thread A belongs to? OK, thanks.

> Note: inheriting QThread is wrong practice and should probably never be done.

Yep, I know.




More information about the Interest mailing list