Emit thread safety



  • @class MyServer: public QTcpServer
    {
    //...
    public:
    MyServer(/.../)
    /.../
    {
    connect(this, SIGNAL(internalShutdown()), SLOT(doShutdown()), Qt::QueuedConnection);
    }
    //...
    void shutdown()
    {
    emit internalShutdown();
    }
    //...
    signals:
    void internalShutdown();
    //...
    private slots:
    void doShutdown();
    //...
    };@
    //==============================================================
    MyServer instance created in main thread context and work with main thread events processing (app.exec()). And now, myServerInstance->shutdown() executed from other thread (not from main thread) -- executed from Control-C Handler (which is executed in temporary thread created by OS). Question is: is it safe that emit internalShutdown() inside MyServer::shutdown() executed in another thread context? (I mean emit keyword safety). Sorry for my English



  • The emit keyword itself doesn't do much. It is there for the looks of it. Essentially emitting a signal is calling a specific function. Signal/slot connections are by default made with the thread auto-detection enabled. The meta object will notice that different threads are being used and automatically a queued connection will be chosen. It is possible that a direct connection is enforced. In that case the called slot is executed in the emitting thread's context.

    I believe there is an article on the wiki explaining this more in detail.

    Bottom line is that signal/slot connections are locked, and therefore thread safe.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.