Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the connect() call thread safe in Qt?

I have two QObjects A and B living in separate QThreads. A will emit a signal while B has a matching slot. I want to use connect() to connect A's signal to B's slot.

So the question is, is the connect() call thread safe? Does it matter in which of the two threads the connect is made?

like image 863
Lennart Rolland Avatar asked Sep 30 '22 18:09

Lennart Rolland


1 Answers

Yes, QObject::connect() is thread safe method:

Note: All functions in this class are reentrant, but connect(), connect(), disconnect(), and disconnect() are also thread-safe.

It doesn't matter from which thread you do the connection. But you should care about using of auto connection(default connection), unique connection or queued connection between your objects. And you should run event loops in both of your threads.

Also I strongly suggest you to check following articles: first, second.

like image 100
Max Go Avatar answered Oct 04 '22 02:10

Max Go