c++qtthread-safety

Qt Signals and slot thread safety


Let's say I have a signal change connected to a slot notify. If the change signal is emitted, the notify slot will start executing.

Now what happens if a second change signal is emitted and the first notify slot didn't finish its execution?

Is the second slot launched concurrently with the first? And if so, is Qt handling the thread-safety or it's up to the programmer to handle it?


Solution

  • It depends on the connection type you specified via calling connect function.

    The only way when a slot will be launched concurrently is if you specified Qt::DirectConnection AND emitting a signal in a thread different from the slot's thread.

    If you omit connection type, it would be Qt::AutoConnection. In this case if you emit a signal from one thread, and catching it in another one (e.g. in main GUI thread) - Qt will put a slot's call to the message queue and will make all calls sequentially.

    Read this for further info: Signals and Slots Across Threads.