Thread: Trouble when try to implement a timer thread using C++ on linuxquestion

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    Trouble when try to implement a timer thread using C++ on linuxquestion

    These days I am trying to implement a digital clock thread using C++ in my embedded linux platform, its kernel version is 2.4. My purpose is to display a clock at the corner of OSD page, of course it will change its time every second.

    We use class gui object to draw OSD UI and currently our implementation is below:

    Code:
    class gui
    {
    public:
       void Start_timer_thread()
       {
         pthread_t clock_thread_id;           
         int ret = -1;
        
         /* create an external thread to deal with it */
         ret = pthread_create(&clock_thread_id, NULL, gui::digital_clock_thread, this);
         if (ret != 0)
         {
         fprintf (stderr, "Cannot spawn thread. err = %d\n", ret);
         return RM_ERROR;
         }
       }
      
       static void* digital_clock_thread(void *ptr)
       {
         /*
          * Since it shoud be a static function according to C++ designing principle,
          * we need to do static cast in order to use the following non-static functions
          * and variables, it seems be a common thread to use in this occasion, I will learn
          * it from linuxquestion and thanks very much.
          *
          **/
         (static_cast<gui*>(ptr))->real_digital_clock_thread();
         return NULL;     
       }
    
       void real_digital_clock_thread()
       {
          RMuint32 page_id = 0;
          RMstatus status = RM_ERROR;
          struct sigaction new_act, old_act;  
          struct itimerval itv, oldtv;
    
         /*
          * Using sigaction instead of signal in order that signal handler can be restored.
          *
          **/         
          /* signal(SIGALRM, SigAlarmHandler); */
          new_act.sa_handler = SigAlarmHandler;
          sigaction(SIGALRM, &new_act, &old_act);
     
          /* set time interval */
          itv.it_interval.tv_sec = 1;
          itv.it_interval.tv_usec = 0;
          itv.it_value.tv_sec = 1;
          itv.it_value.tv_usec = 0;
          setitimer(ITIMER_REAL, &itv, &oldtv);
    
          while (1)
          {
            if(thread_exit_flag)
            {
                /* it has changed to another page */
                signal(SIGALRM, old_act.sa_handler);
                exit(0);
            }
            pause();
          } 
       }
        
       static void SigAlarmHandler(int signal)
       {
         /*
          * I try to use the former method to do non-static cast, but compiling error.
          * For static function, it doesn't have idea of this pointer, it is globle.
          *
          **/
          /* SigAlarmHandler_StaticCast(this); */
          /* (static_cast<gui*>(this))->real_SigAlarmHandler(); */
          this->real_SigAlarmHandler();
       }
    
       void  SigAlarmHandler_StaticCast(gui* ptr)
       {
         (static_cast<gui*>(ptr))->real_SigAlarmHandler();
       }
    
       void  real_SigAlarmHandler()
       {
          time_t t;
          struct tm *st;
        
          time(&t);  
          printf("timer signal..\n");
          st = localtime(&t);   
          /*
           * there are many non static functions and variables which are already existed and I want to use them
           * to draw on OSD.
           */
          draw_timer_numbers_on_OSD(st);   
       }
      
    private:
      
       void draw_timer_numbers_on_OSD(struct tm *st)
       {
           printf("class gui. draw_timer_numbers_on_OSD.....\n");   
           ......
       }
         
    };
    Thanks for your patient to read over my source code. Inside source code it includes my design principle and my whole program structure, also my difficulty which compiling error, signal handler function is static, but I want to use those non static functions and variables to draw on OSD. This is my troublesome and what do I turn for help. Thanks very much in advance.

    I have also tried the following methods to accomplish my goal.

    1. Since in current step I am trying to find a non-static function to do signal dealing with issue, manage to implement its signal handler out of class gui, using "extern "C" to write a global function, but the problem is that in this function we can't use the methods and variables inside class gui or if you know how to use it, please let me know and thanks very much in advance.

    2. Search in google and find that it is possible to change signal to expection and then to catch it. It seems some difficult and don't know whether it is really workable.

    3. Instead of using signal SIGALRM and its signal handler difficult, we try to use sleep function, sleep 1 second every time and then to update timer, you know uClinux isn't real time and drawing liquid timer number will be also need some time, after all it is embedded, so its precision is also not good.

    I am not very good at C++ programming, maybe this program can be solved out nicely on my current program design framework, or I need to rewrite it completely. Please give me some advise and discussion are also welcome.

    Thanks very much for your entire kind patient.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    http://linux-documentation.com/en/pa...#Nonreentrancy
    Signal handlers run in a restricted environment. You can't safely use the entire 'C' library API.

    For each function you intend to call, you need to make sure it's safe to call from a signal handler. If it doesn't say, then assume it isn't.

    To get something like a smooth clock, I would suggest say refreshing the time every 250ms. Since, as you say, there is no way to guarantee the 1s tick, this strikes a balance between nice smooth update, and not thrashing the machine drawing the time continuously.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    Thanks for your kindly reply.

    According to your experience and method, it is not widely to use signal handler to implement, more better way is to use function usleep, sleep 250ms every time and then to update.

    So in the opinon of those language designer, it won't be good idea to write so many functions in the signal handler, am I right?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think that using "signal" handler is a bad idea in this case, since there is a much better alternative. Like you say, usleep() will do a good job.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    Quote Originally Posted by matsp View Post
    I think that using "signal" handler is a bad idea in this case, since there is a much better alternative. Like you say, usleep() will do a good job.

    --
    Mats
    Thanks very much for your reply.

    At first I have the idea to use SIGALRM since I find signal emit will be real time, but sleep will cause CPU ideal and waste resource, and at the same time sleep or usleep is also impemented by sending alarm signal periodly. When meeting difficulities, then I find signal method on this occasion isn't a good idea.
    Last edited by steven_yu; 12-20-2007 at 08:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make a thread sleep or std::recv timeout?
    By BrianK in forum Linux Programming
    Replies: 3
    Last Post: 02-26-2003, 10:27 PM
  2. Multithreading
    By Cela in forum Windows Programming
    Replies: 13
    Last Post: 01-15-2003, 03:02 PM
  3. Your Best thread and your most stupid thread ??
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 01-03-2003, 07:41 PM
  4. MFC Controls and Thread Safety :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 12-06-2002, 11:36 AM
  5. Multi-Thread Programming
    By drdroid in forum C++ Programming
    Replies: 6
    Last Post: 04-04-2002, 02:53 PM