timer_delete()

Hi,

I am having problems with timer_delete(). A timer is create that periodically sends a signal to be handled by a function. There are 2 other threads running with the main thread that holds the timer. The main thread loops through a usleep() call while the function that is trigered runs every 1ms.

iret = timer_create(CLOCK_REALTIME, &event, &timerid);
if (iret == -1){
perror(“Problem with timer_create.”);
return 0;}
// Setup the clock update rate using the itimerspec structure
itimer.it_value.tv_sec = 1;
itimer.it_value.tv_nsec = 500000;
itimer.it_interval.tv_nsec = 1000000;
itimer.it_interval.tv_sec = 0;
timer_settime(timerid, 0, &itimer, NULL);

  // Spawn the Serial port and Read Keyboard threads

  pthread_create(&IMUthread, NULL, thread_function_imu, NULL);
  pthread_getschedparam( IMUthread, &IMUpolicy, &IMUSchParam);
  IMUSchParam.sched_priority = 12;
  pthread_setschedparam( IMUthread,  IMUpolicy, &IMUSchParam);
   
  pthread_create(&GETCthread, NULL, get_keyboard_char, NULL);
  pthread_getschedparam( GETCthread, &GETCpolicy, &GETCSchParam);
  GETCSchParam.sched_priority = 12;
  pthread_setschedparam( GETCthread,  GETCpolicy, &GETCSchParam);

  while(global_running == 1)
  {
    usleep(10)
  }
  
  if (timer_delete(timerid)!= 0)
  {
    perror("========== Error deleting timer ===========");
    exit(1);
  }     

However, when global_running is set to 0, the function that is called by the timer is still called and executed a couple of times before the program actually terminates and this appears to happen AFTER timer_delete is called.

If anybody can point me in the right direction or knows what is causing this it would be greatly appriciated.

Thanks,

Julien

Try pthread_join or conditional variables. I hope this is where you are looking for.

Freddy

Hi,

Thanks for the reply. I am actually using pthread_join to ensure that the other threaps finish their work properly (forgot to include it in the code i put in my post). I’m not sure I understand what you mean when you say to use conditional variables to solve this.

  myPeriod.nsec  = 500000;
  myPeriod.fract = 0;
  
  ClockPeriod(CLOCK_REALTIME,&myPeriod,NULL,0);
  
  /*  Set the priority on the main thread to 20. 10 is normal. */
  MAINthread=pthread_self();
  pthread_getschedparam( MAINthread, &MAINpolicy, &MAINSchParam);
  MAINSchParam.sched_priority = 20;
  pthread_setschedparam( MAINthread,  MAINpolicy, &MAINSchParam);
  ThreadCtl(_NTO_TCTL_IO, 0);
  
  event.sigev_notify = SIGEV_SIGNAL; 
  event.sigev_signo = SIGUSR1;
  signal(SIGUSR1,ControlLoop);
   
  iret = timer_create(CLOCK_REALTIME, &event, &timerid);
  if (iret == -1){
    perror("Problem with timer_create.");
    return 0;}
  //    Setup the clock update rate using the itimerspec structure
  itimer.it_value.tv_sec = 1;
  itimer.it_value.tv_nsec     =  500000;         
  itimer.it_interval.tv_nsec  =  1000000;
  itimer.it_interval.tv_sec   = 0;
  timer_settime(timerid, 0, &itimer, NULL);         //START TIMER
   
  // Spawn the Serial port and Read Keyboard threads
  pthread_create(&IMUthread, NULL, thread_function_imu, NULL);
  pthread_getschedparam( IMUthread, &IMUpolicy, &IMUSchParam);
  IMUSchParam.sched_priority = 12;
  pthread_setschedparam( IMUthread,  IMUpolicy, &IMUSchParam);
   
  pthread_create(&GETCthread, NULL, get_keyboard_char, NULL);
  pthread_getschedparam( GETCthread, &GETCpolicy, &GETCSchParam);
  GETCSchParam.sched_priority = 12;
  pthread_setschedparam( GETCthread,  GETCpolicy, &GETCSchParam);
  
   
  while(global_running == 1)
  {
    usleep(10) 
   }
  
  if (timer_delete(timerid)!= 0)
  {
    perror("========== Error deleting timer ===========");
    exit(1);
  }
 
  pthread_join(IMUthread,NULL);
  pthread_join(GETCthread,NULL);

The other two threads terminate as desired, however the function ControlLoop is still executed a couple times after timer_delete() is called. Is this normal?

Thanks,

Julien