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. More single signals vs one signal with data

More single signals vs one signal with data

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 489 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.
  • guerinoniG Offline
    guerinoniG Offline
    guerinoni
    wrote on last edited by
    #1

    Hi, something I ask me if it is better to emit five or ten or many signals when every property is modified or maybe is better emit only one signal with a vector of property as an argument. Which solution is more expensive in terms of performance and code flexibility?

    Pablo J. RoginaP 1 Reply Last reply
    0
    • guerinoniG guerinoni

      Hi, something I ask me if it is better to emit five or ten or many signals when every property is modified or maybe is better emit only one signal with a vector of property as an argument. Which solution is more expensive in terms of performance and code flexibility?

      Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by Pablo J. Rogina
      #2

      @guerinoni what will you do when only one of the ten properties is updated, and you have only one signal with a vector?
      are you going to emit that signal with the vector having the old nine values plus the new value?
      How will somebody connected to such signal know about what property did indeed change?

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      3
      • rrlopezR Offline
        rrlopezR Offline
        rrlopez
        wrote on last edited by
        #3

        Hello @guerinoni ,
        I would not recommend doing a vector of signals for several reasons.
        Lets say you are going to emit the following signal:

        vector = [a, b, c]
        emit signalVector[vector]
        

        Every slot you would want to connect to this signal would then have to receive the vector argument. This in my opinion has several drawbacks:

        1. What if one of your slots will be using only the b value, ignoring a and c? Then you have two unused values everytime the signal is triggered.
        2. Is every property going to change when you emit this signal? If not, you are sending duplicate data to your listeners, which is never a good thing.
        3. How are you going to access each property of the vector? By index? If so, your code is not very flexible because you would then need to have a strict structure on how to add these properties to the vector.

        Number 3 could be solved by not using a vector, but multiple parameters:

        signalVector(int a, int b, int c, ...)
        

        However, this appoach has its drawbacks as well (adding up number 1 and number 2 from the previous approach as well):

        1. Each time you would want to add a new property to the vector, you will have to modify all slots and connections to this signal, which is not a good design.

        NOTE: if every property you plan on clustering up is related (i.e. you need all 3 to determine a state change or anything else), then I think this second approach is better, because all of the variables are going to be used every time to determine what needs to be done and your signal and slots will not grow for every other unrelated property.

        Using individual signals for each property will indeed generate more calls for slots, however, I believe the performance decay will not be as much as you think it could be.
        It is up to you to evaluate if the drawbacks presented for the previous implementations would not be an issue for you.

        I hope this clarifies your mind about this issue.

        Lic-Ing. Rodrigo Lopez Gonzalez
        Embedded Software Engineer
        RidgeRun Engineering Ltd.
        www.ridgerun.com
        Email: rodrigo.lopez@ridgerun.com

        kshegunovK 1 Reply Last reply
        3
        • rrlopezR rrlopez

          Hello @guerinoni ,
          I would not recommend doing a vector of signals for several reasons.
          Lets say you are going to emit the following signal:

          vector = [a, b, c]
          emit signalVector[vector]
          

          Every slot you would want to connect to this signal would then have to receive the vector argument. This in my opinion has several drawbacks:

          1. What if one of your slots will be using only the b value, ignoring a and c? Then you have two unused values everytime the signal is triggered.
          2. Is every property going to change when you emit this signal? If not, you are sending duplicate data to your listeners, which is never a good thing.
          3. How are you going to access each property of the vector? By index? If so, your code is not very flexible because you would then need to have a strict structure on how to add these properties to the vector.

          Number 3 could be solved by not using a vector, but multiple parameters:

          signalVector(int a, int b, int c, ...)
          

          However, this appoach has its drawbacks as well (adding up number 1 and number 2 from the previous approach as well):

          1. Each time you would want to add a new property to the vector, you will have to modify all slots and connections to this signal, which is not a good design.

          NOTE: if every property you plan on clustering up is related (i.e. you need all 3 to determine a state change or anything else), then I think this second approach is better, because all of the variables are going to be used every time to determine what needs to be done and your signal and slots will not grow for every other unrelated property.

          Using individual signals for each property will indeed generate more calls for slots, however, I believe the performance decay will not be as much as you think it could be.
          It is up to you to evaluate if the drawbacks presented for the previous implementations would not be an issue for you.

          I hope this clarifies your mind about this issue.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4

          @rrlopez said in More single signals vs one signal with data:

          if every property you plan on clustering up is related (i.e. you need all 3 to determine a state change or anything else), then I think this second approach is better, because all of the variables are going to be used every time to determine what needs to be done and your signal and slots will not grow for every other unrelated property.

          That almost certainly warrants a dedicated struct spanning these properties, though. So you emit one instance of said struct and modify it as a whole.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          4
          • Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on last edited by
            #5

            The answer is [drum roll please]...it depends on your use case. sending a single signal is more efficient than sending several, but if in fact each entity is signal worthy then they should be discrete signals. A common way of IPC in my world is a shared memory region that is mutex guarded. Any task can read atomic values from that region but only the holder of the mutex can update any entries. It all boils down to use case justification.

            1 Reply Last reply
            1
            • guerinoniG Offline
              guerinoniG Offline
              guerinoni
              wrote on last edited by
              #6

              @Pablo-J-Rogina @rrlopez @kshegunov @Kent-Dorfman
              Many thanks for your explanation, maybe in my this case I can emit all 3 struct as a single signal with a vector of parameters, but my question was for any possible case when I have to choose between emit 100 signal or one signal with 100 parameters. I'll keep in mind these considerations.

              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