Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Emit thread safety

    General and Desktop
    2
    2
    5224
    Loading More Posts
    • 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
      flavio last edited by

      @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 Reply Quote 0
      • F
        Franzk last edited by

        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 Reply Quote 0
        • First post
          Last post