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. How to manage signals in QThread
Forum Updated to NodeBB v4.3 + New Features

How to manage signals in QThread

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 5 Posters 1.7k 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.
  • M Offline
    M Offline
    masa4
    wrote on last edited by
    #1

    I have a thread in runs a while loop:

    void Worker::process()
    {
        while(1){
            
            if(myVar == 2)
            {
    
               ... some codes
              x = true;
            }
            
            else
            {
                x = false;
            }
            emit mySignal(x);
        }
    }
    

    I am connecting mySignal to a slot in another class. And i am manipulating widgets according to state of x. But when i run the program, the ui completly unresponsible. myVar's value can change through program. Now is there any automatic way to manage this situation? Or i need to keep x's old value and with an if condition managing if signal will be emitted or not?

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • M masa4

      I have a thread in runs a while loop:

      void Worker::process()
      {
          while(1){
              
              if(myVar == 2)
              {
      
                 ... some codes
                x = true;
              }
              
              else
              {
                  x = false;
              }
              emit mySignal(x);
          }
      }
      

      I am connecting mySignal to a slot in another class. And i am manipulating widgets according to state of x. But when i run the program, the ui completly unresponsible. myVar's value can change through program. Now is there any automatic way to manage this situation? Or i need to keep x's old value and with an if condition managing if signal will be emitted or not?

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @masa4 said in How to manage signals in QThread:

      while(1){

      If you want signals/slots to work do not use endless loops! This is not how Qt works. Your thread should have an event loop, then there is no need for such loops.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      0
      • jsulmJ jsulm

        @masa4 said in How to manage signals in QThread:

        while(1){

        If you want signals/slots to work do not use endless loops! This is not how Qt works. Your thread should have an event loop, then there is no need for such loops.

        M Offline
        M Offline
        masa4
        wrote on last edited by
        #3

        @jsulm Can u be more specific? I want to emit signals when values changed.

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Emitting signals without an event loop does work, but it's not possible to get a slot called in a thread (from another thread over queued connection) where no event loop is running.
          I still would not run an endless loop here - how does myVar changes at all in your code?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          M 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            Emitting signals without an event loop does work, but it's not possible to get a slot called in a thread (from another thread over queued connection) where no event loop is running.
            I still would not run an endless loop here - how does myVar changes at all in your code?

            M Offline
            M Offline
            masa4
            wrote on last edited by
            #5

            @Christian-Ehrlicher I am getting myVar value from another class(Non qt) which sets this value in a thread. This thread keep checking if computer connected to a device(serial comm). If its, it sets it to 2 and i am setting x(isConnected) to true.

            JonBJ 1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Then better use a QTimer to check for a change every x seconds - then you don't even need another thread for it.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              M 1 Reply Last reply
              2
              • M masa4

                @Christian-Ehrlicher I am getting myVar value from another class(Non qt) which sets this value in a thread. This thread keep checking if computer connected to a device(serial comm). If its, it sets it to 2 and i am setting x(isConnected) to true.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @masa4
                Your loop is too "busy", not to mention that it may not be thread-safe (how do you synchronise access to myVar?).

                Maybe just a QTimer here for occasional/regular polling might be better?

                M 1 Reply Last reply
                2
                • Christian EhrlicherC Christian Ehrlicher

                  Then better use a QTimer to check for a change every x seconds - then you don't even need another thread for it.

                  M Offline
                  M Offline
                  masa4
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher But if i use a timer, lets say run it every 1second or maybe 100ms. Every 100ms i will check the myVar variable, and i will change something on ui. For ex. one thing is i will make disabled one of the checkbox if x==false. But when myvar != 2 but x is still true because 100ms interval didnt finish it yet, and i clicked the checkbox because its still enabled, this situation may creates bugs/crashes? Im not sure if i explained well and i really dont sure about it. Does using timer can cause any problems like that?

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @masa4
                    Your loop is too "busy", not to mention that it may not be thread-safe (how do you synchronise access to myVar?).

                    Maybe just a QTimer here for occasional/regular polling might be better?

                    M Offline
                    M Offline
                    masa4
                    wrote on last edited by
                    #9

                    @JonB said in How to manage signals in QThread:

                    Your loop is too "busy", not to mention that it may not be thread-safe (how do you synchronise access to myVar?).

                    I just read it from another class that communicates with a device. I dont have much idea about synhronization. And what time interval is ok for timer do you know? @jsulm indicated QTimer::start(0) is not a very good idea. Does 100ms reasonable?

                    1 Reply Last reply
                    0
                    • M masa4

                      I have a thread in runs a while loop:

                      void Worker::process()
                      {
                          while(1){
                              
                              if(myVar == 2)
                              {
                      
                                 ... some codes
                                x = true;
                              }
                              
                              else
                              {
                                  x = false;
                              }
                              emit mySignal(x);
                          }
                      }
                      

                      I am connecting mySignal to a slot in another class. And i am manipulating widgets according to state of x. But when i run the program, the ui completly unresponsible. myVar's value can change through program. Now is there any automatic way to manage this situation? Or i need to keep x's old value and with an if condition managing if signal will be emitted or not?

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #10

                      @masa4 this is a very bad idea, besides what th others said, you are currently emitting a signal, basically, every cpu cycle. That is overwhelming your event loop on the main thread.

                      If you insist on this approach, store the previous true/false state somewhere and only emit the signal if /when the state changes.


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      M 1 Reply Last reply
                      3
                      • M masa4

                        @Christian-Ehrlicher But if i use a timer, lets say run it every 1second or maybe 100ms. Every 100ms i will check the myVar variable, and i will change something on ui. For ex. one thing is i will make disabled one of the checkbox if x==false. But when myvar != 2 but x is still true because 100ms interval didnt finish it yet, and i clicked the checkbox because its still enabled, this situation may creates bugs/crashes? Im not sure if i explained well and i really dont sure about it. Does using timer can cause any problems like that?

                        Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @masa4 said in How to manage signals in QThread:

                        For ex. one thing is i will make disabled one of the checkbox if x==false. But when myvar != 2 but x is still true because 100ms interval didnt finish it yet, and i clicked the checkbox because its still enabled,

                        I don't see a problem here - check the real value just before you execute the action on your external lib again - and even then there might be a race condition because the value may change in the few milliseconds between the check and the acutal implementation.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        2
                        • J.HilkJ J.Hilk

                          @masa4 this is a very bad idea, besides what th others said, you are currently emitting a signal, basically, every cpu cycle. That is overwhelming your event loop on the main thread.

                          If you insist on this approach, store the previous true/false state somewhere and only emit the signal if /when the state changes.

                          M Offline
                          M Offline
                          masa4
                          wrote on last edited by
                          #12

                          @J-Hilk Now I started doing in this way with storing old value of variables, and if it changed emitting signals. Now it runs ok but a little question. after several minutes of running program, my fans starts making a lot of noise. after stopping program, fans are stop too. One of my cpu core runs at 100% when running program. Any idea why this happens?

                          JonBJ J.HilkJ 2 Replies Last reply
                          0
                          • M masa4

                            @J-Hilk Now I started doing in this way with storing old value of variables, and if it changed emitting signals. Now it runs ok but a little question. after several minutes of running program, my fans starts making a lot of noise. after stopping program, fans are stop too. One of my cpu core runs at 100% when running program. Any idea why this happens?

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #13

                            @masa4 said in How to manage signals in QThread:

                            One of my cpu core runs at 100% when running program. Any idea why this happens?

                            Depends what your code is! Sounds like it's running at 100%, like originally....

                            1 Reply Last reply
                            1
                            • M masa4

                              @J-Hilk Now I started doing in this way with storing old value of variables, and if it changed emitting signals. Now it runs ok but a little question. after several minutes of running program, my fans starts making a lot of noise. after stopping program, fans are stop too. One of my cpu core runs at 100% when running program. Any idea why this happens?

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #14

                              @masa4 like I previously said, your while(1) loop is running full speed all the time -> one core will be at 100 % load for that.


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              M 1 Reply Last reply
                              2
                              • J.HilkJ J.Hilk

                                @masa4 like I previously said, your while(1) loop is running full speed all the time -> one core will be at 100 % load for that.

                                M Offline
                                M Offline
                                masa4
                                wrote on last edited by
                                #15

                                @J-Hilk @JonB But other way how can i continuously get the values of variables? I am really confused now. So threads not usable in this situation, i need to use timer?

                                M Christian EhrlicherC 2 Replies Last reply
                                0
                                • M masa4

                                  @J-Hilk @JonB But other way how can i continuously get the values of variables? I am really confused now. So threads not usable in this situation, i need to use timer?

                                  M Offline
                                  M Offline
                                  masa4
                                  wrote on last edited by
                                  #16

                                  @masa4 If I dont use while(1), i was not able to read variables continuously, so no point in here to use thread?

                                  jsulmJ JonBJ 2 Replies Last reply
                                  0
                                  • M masa4

                                    @masa4 If I dont use while(1), i was not able to read variables continuously, so no point in here to use thread?

                                    jsulmJ Online
                                    jsulmJ Online
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @masa4 As already said: if you read the values continually all the time then your thread will run at 100%. This has nothing to do with threads as such, it has to do with your code: it simply utilises one CPU core at 100%. Your approach is simply wrong. Do not read the state continuously. Read it several times per second (use a timer for that, no need for additional thread), that is enough to update UI. And then do what @Christian-Ehrlicher already suggested: "I don't see a problem here - check the real value just before you execute the action on your external lib again".

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    1
                                    • M masa4

                                      @masa4 If I dont use while(1), i was not able to read variables continuously, so no point in here to use thread?

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by
                                      #18

                                      @masa4
                                      If you insist on reading the variable "continuously" then you are likely to use a CPU core 100%, just like you said you do not want to do. What else would you expect?

                                      If you use a QTimer you may indeed not need a separate thread for this at all.

                                      1 Reply Last reply
                                      1
                                      • M masa4

                                        @J-Hilk @JonB But other way how can i continuously get the values of variables? I am really confused now. So threads not usable in this situation, i need to use timer?

                                        Christian EhrlicherC Online
                                        Christian EhrlicherC Online
                                        Christian Ehrlicher
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        @masa4 said in How to manage signals in QThread:

                                        But other way how can i continuously get the values of variables?

                                        For the third time (or even more): Use a QTimer!

                                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                        Visit the Qt Academy at https://academy.qt.io/catalog

                                        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