Nominate our 2022 Qt Champions!

Is it Ok to emit a signal from a thread?

  • My application is Qt based and has an Open GL widget. It runs under Centos Linux and uses a Linux pthread to capture data. My understanding is that the thread must not do anything that directly or indirectly updates any display.
    Does this same concept apply to emitting signals?
    I suspect that this line of code:
    emit favorite_signal( x );
    Sets a flag that causes the Qt engine to make the function call, provided there is a connect for that signal. If that is the case, it might be okay to emit signals from thread called code.
    I found something that might indirectly answer the question in that signals just call QMetaObject::activate but am uncertain if I interpret that correctly.

  • Signals and slots employ mutexes and can be used to communicate between threads. Just make sure you pass by value or have mutexes to guard data access if passing by other means. I have had issues passing by reference so I am not sure it can be done.

    You can even use slots to communicate to objects inside threads, however you need to be evaluating the Qt event loop inside that thread if you do so.

Log in to reply