Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Emit thread safety
Qt 6.11 is out! See what's new in the release blog

Emit thread safety

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 5.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    flavio
    wrote on last edited by
    #1

    @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

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on last edited by
      #2

      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.

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved