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. When I used RTC on different thread all GUI QT is lock?
Forum Updated to NodeBB v4.3 + New Features

When I used RTC on different thread all GUI QT is lock?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 967 Views 2 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.
  • stackprogramerS Offline
    stackprogramerS Offline
    stackprogramer
    wrote on last edited by
    #1

    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);
    }
    
    
    jsulmJ 1 Reply Last reply
    0
    • stackprogramerS stackprogramer

      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);
      }
      
      
      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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...

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

      stackprogramerS 1 Reply Last reply
      1
      • jsulmJ jsulm

        @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...

        stackprogramerS Offline
        stackprogramerS Offline
        stackprogramer
        wrote on last edited by
        #3

        @jsulm When i use this method all thread is lock when I comment it every thing work correctly...

            setup_rtc();
        
        
        jsulmJ 1 Reply Last reply
        0
        • stackprogramerS stackprogramer

          @jsulm When i use this method all thread is lock when I comment it every thing work correctly...

              setup_rtc();
          
          
          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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...

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

          1 Reply Last reply
          1
          • S Offline
            S Offline
            stryga42
            wrote on last edited by stryga42
            #5

            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...

            jsulmJ 1 Reply Last reply
            0
            • S stryga42

              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...

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

              @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 called

              That'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.

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

              stackprogramerS J.HilkJ 2 Replies Last reply
              0
              • jsulmJ jsulm

                @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 called

                That'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.

                stackprogramerS Offline
                stackprogramerS Offline
                stackprogramer
                wrote on last edited by
                #7

                @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);
                    }
                }
                
                JonBJ jsulmJ 2 Replies Last reply
                0
                • stackprogramerS stackprogramer

                  @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);
                      }
                  }
                  
                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #8

                  @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.

                  1 Reply Last reply
                  1
                  • stackprogramerS stackprogramer

                    @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);
                        }
                    }
                    
                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @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...

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

                    1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @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 called

                      That'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.

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

                      @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.


                      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.

                      stackprogramerS 1 Reply Last reply
                      2
                      • stackprogramerS stackprogramer referenced this topic on
                      • J.HilkJ J.Hilk

                        @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.

                        stackprogramerS Offline
                        stackprogramerS Offline
                        stackprogramer
                        wrote on last edited by
                        #11

                        @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;
                        
                        
                        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