When I used RTC on different thread all GUI QT is lock?
-
When I used RTC on different thread all GUI QT thread and main thread is lock? Button clicks is not works.... Why this problem is caused in my thread....
#include "rtc.h" int rtc_fd; void rtc_interrupt_handler(int signum) { // Handle RTC interrupt qDebug()<<"INT is trigered"; delayMilliSeconds(1000); QCoreApplication::processEvents(QEventLoop::AllEvents,30); } void stop_rtc(int signum) { // Close RTC device file close(rtc_fd); printf("RTC stopped.\n"); exit(EXIT_SUCCESS); } void setup_rtc() { struct itimerval it_val; struct rtc_time rtc_tm; // Open RTC device file rtc_fd = open(RTC_DEVICE, O_RDONLY); if (rtc_fd == -1) { perror("Error opening RTC device file"); exit(EXIT_FAILURE); } // Read current time from RTC if (ioctl(rtc_fd, RTC_RD_TIME, &rtc_tm) == -1) { perror("Error reading RTC time"); exit(EXIT_FAILURE); } // Configure RTC alarm to trigger every 1 millisecond rtc_tm.tm_sec += 1; if (rtc_tm.tm_sec >= 60) { rtc_tm.tm_sec = 0; rtc_tm.tm_min += 1; if (rtc_tm.tm_min >= 60) { rtc_tm.tm_min = 0; rtc_tm.tm_hour += 1; if (rtc_tm.tm_hour >= 24) { rtc_tm.tm_hour = 0; } } } if (ioctl(rtc_fd, RTC_ALM_SET, &rtc_tm) == -1) { perror("Error setting RTC alarm"); exit(EXIT_FAILURE); } // Register RTC interrupt handler signal(SIGALRM, rtc_interrupt_handler); // Enable RTC alarm interrupts it_val.it_value.tv_sec = 0; it_val.it_value.tv_usec = 1000; it_val.it_interval = it_val.it_value; if (setitimer(ITIMER_REAL, &it_val, NULL) == -1) { perror("Error enabling RTC interrupts"); exit(EXIT_FAILURE); } } RTC::RTC(QString s) : name(s) { // Setup RTC } void RTC::run() { setup_rtc(); while(rtcFlag==1) { // qDebug()<<"RTC is trigered"; delayMilliSeconds(1000); QCoreApplication::processEvents(QEventLoop::AllEvents,30); } stop_rtc(0); }
-
When I used RTC on different thread all GUI QT thread and main thread is lock? Button clicks is not works.... Why this problem is caused in my thread....
#include "rtc.h" int rtc_fd; void rtc_interrupt_handler(int signum) { // Handle RTC interrupt qDebug()<<"INT is trigered"; delayMilliSeconds(1000); QCoreApplication::processEvents(QEventLoop::AllEvents,30); } void stop_rtc(int signum) { // Close RTC device file close(rtc_fd); printf("RTC stopped.\n"); exit(EXIT_SUCCESS); } void setup_rtc() { struct itimerval it_val; struct rtc_time rtc_tm; // Open RTC device file rtc_fd = open(RTC_DEVICE, O_RDONLY); if (rtc_fd == -1) { perror("Error opening RTC device file"); exit(EXIT_FAILURE); } // Read current time from RTC if (ioctl(rtc_fd, RTC_RD_TIME, &rtc_tm) == -1) { perror("Error reading RTC time"); exit(EXIT_FAILURE); } // Configure RTC alarm to trigger every 1 millisecond rtc_tm.tm_sec += 1; if (rtc_tm.tm_sec >= 60) { rtc_tm.tm_sec = 0; rtc_tm.tm_min += 1; if (rtc_tm.tm_min >= 60) { rtc_tm.tm_min = 0; rtc_tm.tm_hour += 1; if (rtc_tm.tm_hour >= 24) { rtc_tm.tm_hour = 0; } } } if (ioctl(rtc_fd, RTC_ALM_SET, &rtc_tm) == -1) { perror("Error setting RTC alarm"); exit(EXIT_FAILURE); } // Register RTC interrupt handler signal(SIGALRM, rtc_interrupt_handler); // Enable RTC alarm interrupts it_val.it_value.tv_sec = 0; it_val.it_value.tv_usec = 1000; it_val.it_interval = it_val.it_value; if (setitimer(ITIMER_REAL, &it_val, NULL) == -1) { perror("Error enabling RTC interrupts"); exit(EXIT_FAILURE); } } RTC::RTC(QString s) : name(s) { // Setup RTC } void RTC::run() { setup_rtc(); while(rtcFlag==1) { // qDebug()<<"RTC is trigered"; delayMilliSeconds(1000); QCoreApplication::processEvents(QEventLoop::AllEvents,30); } stop_rtc(0); }
@stackprogramer said in When I used RTC on different thread all GUI QT is lock?:
Why this problem is caused in my thread....
You're doing threading wrongly. You're blocking the main thread somewhere. But from the code you posted it is not possible to say where exactly...
-
@stackprogramer said in When I used RTC on different thread all GUI QT is lock?:
Why this problem is caused in my thread....
You're doing threading wrongly. You're blocking the main thread somewhere. But from the code you posted it is not possible to say where exactly...
@jsulm When i use this method all thread is lock when I comment it every thing work correctly...
setup_rtc();
-
@jsulm When i use this method all thread is lock when I comment it every thing work correctly...
setup_rtc();
@stackprogramer Again: you're blocking your main thread, but you do not show how you actually use RTC! I doubt you really execute it in another thread, but as long as you refuse to show how you use it I have nothing more to say...
-
Please share with us:
- source code of setup_rtc()
- the piece of code from where setup_rtc() is called
We are not gifted with the ability of second sight :-)
A general advice: A forced delay of one second inside an interrupt handler usually is not a good idea since interrupt handler are expected to return fast or they may block other, lower priority interrupts.
A more special advice: To perform a timed delay inside the interrupt handler of the real time clock may not work altogether, if the delay is timed by the RTC which interrupt handler you are blocking...
-
Please share with us:
- source code of setup_rtc()
- the piece of code from where setup_rtc() is called
We are not gifted with the ability of second sight :-)
A general advice: A forced delay of one second inside an interrupt handler usually is not a good idea since interrupt handler are expected to return fast or they may block other, lower priority interrupts.
A more special advice: To perform a timed delay inside the interrupt handler of the real time clock may not work altogether, if the delay is timed by the RTC which interrupt handler you are blocking...
@stryga42 said in When I used RTC on different thread all GUI QT is lock?:
source code of setup_rtc()
the piece of code from where setup_rtc() is calledThat's already posted. The question is more: how is this class used? Is it really running in another thread? I'm quite sure it does not.
-
@stryga42 said in When I used RTC on different thread all GUI QT is lock?:
source code of setup_rtc()
the piece of code from where setup_rtc() is calledThat's already posted. The question is more: how is this class used? Is it really running in another thread? I'm quite sure it does not.
@jsulm @stryga42
Another section code is GUI and a button, I think I should append a delay function. Other sections are not necessary. GUI code is not causing this problem. After Thinking I concluded that this definition force that interrupts RTC is executed on the main thread. But I should refactor the code to solve it.//Delay function defintion static void delayMilliSeconds(int millisecondsToWait) { QTime dieTime = QTime::currentTime().addMSecs(millisecondsToWait); while (QTime::currentTime() < dieTime) { QCoreApplication::processEvents(QEventLoop::AllEvents,30); } }
-
@jsulm @stryga42
Another section code is GUI and a button, I think I should append a delay function. Other sections are not necessary. GUI code is not causing this problem. After Thinking I concluded that this definition force that interrupts RTC is executed on the main thread. But I should refactor the code to solve it.//Delay function defintion static void delayMilliSeconds(int millisecondsToWait) { QTime dieTime = QTime::currentTime().addMSecs(millisecondsToWait); while (QTime::currentTime() < dieTime) { QCoreApplication::processEvents(QEventLoop::AllEvents,30); } }
@stackprogramer
This is a very bad idea, whatever you trying to address with it.If you need a delay for some reason the Qt way is to set a single shot timer and do whatever you were going to do after the delay from the slot for timer expiry.
-
@jsulm @stryga42
Another section code is GUI and a button, I think I should append a delay function. Other sections are not necessary. GUI code is not causing this problem. After Thinking I concluded that this definition force that interrupts RTC is executed on the main thread. But I should refactor the code to solve it.//Delay function defintion static void delayMilliSeconds(int millisecondsToWait) { QTime dieTime = QTime::currentTime().addMSecs(millisecondsToWait); while (QTime::currentTime() < dieTime) { QCoreApplication::processEvents(QEventLoop::AllEvents,30); } }
@stackprogramer said in When I used RTC on different thread all GUI QT is lock?:
I think I should append a delay function
Append where and why?
I hope not in the GUI thread... -
@stryga42 said in When I used RTC on different thread all GUI QT is lock?:
source code of setup_rtc()
the piece of code from where setup_rtc() is calledThat's already posted. The question is more: how is this class used? Is it really running in another thread? I'm quite sure it does not.
@jsulm said in When I used RTC on different thread all GUI QT is lock?:
Is it really running in another thread? I'm quite sure it does not.
I'm 100% sure it doesn't.
Especially as the horrible, horrible
QCoreApplication::processEvents
calls are made inside the "thread" which makes negative sense and would at best be a race condition. -
S stackprogramer referenced this topic on
-
@jsulm said in When I used RTC on different thread all GUI QT is lock?:
Is it really running in another thread? I'm quite sure it does not.
I'm 100% sure it doesn't.
Especially as the horrible, horrible
QCoreApplication::processEvents
calls are made inside the "thread" which makes negative sense and would at best be a race condition.@J-Hilk @Jsum @all Thanks very much Finally I concluded that:
Case one: my tread class that run to interrupt, Qthread was dynamic but interrupt need be static. For overcoming this problem I used a singleton design pattern.
Case two:
it is not related to GUI main thread, I had a /dev/rtc0 in Linux kernel, and I used it in my application now we examine it, we concluded that We need to slow down the rtc interrupt, I changed the interrupt time from 1 ms to 60ms now everything is ok and GUI main thread works correctly.RTC* RTC::instance = nullptr;