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. [SOLVED]Safety of references as arguments to emitted signals
QtWS25 Last Chance

[SOLVED]Safety of references as arguments to emitted signals

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.8k Views
  • 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.
  • D Offline
    D Offline
    DRoscoe
    wrote on last edited by DRoscoe
    #1

    In a single-threaded application, is it safe to pass an argument by reference when emitting a signal? I'm guessing it isn't, but would like confirmation.

    JKSHJ 1 Reply Last reply
    0
    • D DRoscoe

      In a single-threaded application, is it safe to pass an argument by reference when emitting a signal? I'm guessing it isn't, but would like confirmation.

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #2

      @DRoscoe said:

      In a single-threaded application, is it safe to pass an argument by reference when emitting a signal? I'm guessing it isn't, but would like confirmation.

      It's perfectly safe in both single- and multi-threaded applications. Notice that, for example, QLineEdit emits the textChanged() signal with a const QString& argument.

      In a single-threaded application, the default connection type is a Qt::DirectConnection. When the signal is emitted, the connected slots are immediately called one after the other, with the signal argument passed directly to each slot. In other words, the top-level function which emitted the signal is blocked until all the slots have returned.

      When connections are made between different threads, the default connection type is a Qt::QueuedConnection. (You can manually choose this type for connections within the same thread, if you wish.) When the signal is emitted, the signal arguments are copied into an internal buffer, and the top-level function continues immediately. The slots are then called at some later time, when their threads' event loops are ready to process signals. The copied arguments are passed to each slot -- your original reference is not used anymore.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      2
      • D Offline
        D Offline
        DRoscoe
        wrote on last edited by DRoscoe
        #3

        Whoa! I didn't realize that the call to slots block on a single-threaded app. That is not what I expected. Coming from the ACE world, I had assumed it followed more of a Reactor Pattern with an activation schedule. This simulates multi-threaded behavior in a single-threaded application. This could explain some of the sluggish responsiveness I've noticed. In case you can't tell, Qt is not my native language :-)

        If I were to change the connection type to be more like what I expect, then I'd be incurring the cost of making copies? In ACE, it uses a reference-counted pointer to encapsulate message payloads.

        Thanks!

        JKSHJ 1 Reply Last reply
        0
        • D DRoscoe

          Whoa! I didn't realize that the call to slots block on a single-threaded app. That is not what I expected. Coming from the ACE world, I had assumed it followed more of a Reactor Pattern with an activation schedule. This simulates multi-threaded behavior in a single-threaded application. This could explain some of the sluggish responsiveness I've noticed. In case you can't tell, Qt is not my native language :-)

          If I were to change the connection type to be more like what I expect, then I'd be incurring the cost of making copies? In ACE, it uses a reference-counted pointer to encapsulate message payloads.

          Thanks!

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @DRoscoe said:

          Whoa! I didn't realize that the call to slots block on a single-threaded app. That is not what I expected. Coming from the ACE world, I had assumed it followed more of a Reactor Pattern with an activation schedule. This simulates multi-threaded behavior in a single-threaded application.

          Qt::QueuedConnection will give you the behaviour you expect.

          This could explain some of the sluggish responsiveness I've noticed. In case you can't tell, Qt is not my native language :-)

          I wouldn't expect sluggishness unless (i) the signal is emitting at a high frequency, and (ii) your slot takes a long time to return ("long" is relative here). If (ii) is true, you might want to consider delegating its task to a secondary thread.

          We were all non-"native" at some point in our careers :)

          If I were to change the connection type to be more like what I expect, then I'd be incurring the cost of making copies? In ACE, it uses a reference-counted pointer to encapsulate message payloads.

          Qt uses reference-counting on many of its datatypes too (e.g. QString, QVector<T>), where "copy" == "shallow copy" == "increment reference counter". See Implicit Sharing for details.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          1

          • Login

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