How to manage signals in QThread
-
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
mySignalto a slot in another class. And i am manipulating widgets according to state ofx. 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 keepx's old value and with an if condition managing if signal will be emitted or not? -
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
mySignalto a slot in another class. And i am manipulating widgets according to state ofx. 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 keepx's old value and with an if condition managing if signal will be emitted or not?@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.
-
@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.
-
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? -
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?@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.
-
Then better use a QTimer to check for a change every x seconds - then you don't even need another thread for it.
-
@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.
-
Then better use a QTimer to check for a change every x seconds - then you don't even need another thread for it.
@Christian-Ehrlicher But if i use a timer, lets say run it every 1second or maybe 100ms. Every 100ms i will check the
myVarvariable, and i will change something on ui. For ex. one thing is i will make disabled one of the checkbox ifx==false. But whenmyvar != 2but 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? -
@masa4
Your loop is too "busy", not to mention that it may not be thread-safe (how do you synchronise access tomyVar?).Maybe just a
QTimerhere for occasional/regular polling might be better?@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? -
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
mySignalto a slot in another class. And i am manipulating widgets according to state ofx. 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 keepx's old value and with an if condition managing if signal will be emitted or not?@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.
-
@Christian-Ehrlicher But if i use a timer, lets say run it every 1second or maybe 100ms. Every 100ms i will check the
myVarvariable, and i will change something on ui. For ex. one thing is i will make disabled one of the checkbox ifx==false. But whenmyvar != 2but 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?@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.
-
@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.
@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-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-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?
-
@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.
-
@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?
-
@masa4 If I dont use while(1), i was not able to read variables continuously, so no point in here to use thread?
@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".
-
@masa4 If I dont use while(1), i was not able to read variables continuously, so no point in here to use thread?
-
@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?
@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!