title: C timers and threads again
body: <p>Can someone help me to complete my code with a function that can check the result of a timer "check_timer" and another one that reset this timer if it had expired "reset_timer"? </p>
  
  <pre><code>#include &lt;pthread.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;sys/time.h&gt;
  
  #define GLOBAL_TIMER  8 * 60 * 60
  
  
  typedef struct protocol_type {
   int protocol_number;
   char *protocol_name;
   pthread_t thread_timer_id;
  } protocol_type_t;
  
  pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
  pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  
  
  /* call back function - inform the user the time has expired */
  void timeout_call_back(pthread_t thread_id)
  {
      printf("Welcome thread %ld, your time is up ===\n", pthread_self());
      // doing some other stuff
  }
  
  /* Go to sleep for a period of seconds */
  static void* start_timer(void *args)
  {
      /* function pointer */
      void (*finish_function)(pthread_t);
  
  
      int seconds = *((int*) args);
  
      finish_function = timeout_call_back;
  
   struct timeval now;
   struct timespec timeout;
  
   pthread_mutex_lock(&amp;mut);
   printf("thread ID : %ld, are waiting for %d seconds to to expire\n", pthread_self(), seconds);
   gettimeofday(&amp;now, NULL);
   timeout.tv_sec = now.tv_sec + seconds;
   timeout.tv_nsec = now.tv_usec * 1000;
   pthread_cond_timedwait(&amp;cond, &amp;mut, &amp;timeout);
   (*finish_function)(pthread_self());
   pthread_mutex_unlock(&amp;mut);
      pthread_exit(NULL);
  }
  
  // This function is in MT environnement and is running inside a daemon
  int received_buffer_parser(char *received_buffer) {
   pthread_mutex_t mut_main = PTHREAD_MUTEX_INITIALIZER; 
   protocol_type_t *my_protocol;
  
   // Identify protocol type
   my_protocol = protocol_identifier(received_buffer);
  
   // Check if i received it in the last 8 hours in safe 
   pthread_mutex_lock(&amp;mut_main);
      if (check_timer(my_protocol-&gt;thread_id) has expired) { // I want to write this function
    // I want to reset thread timer
     launch_new_timer(my_protocol-&gt;thread_id)
   }
   else {
    // doing some stuff
    // dump data to the disk
   }
   pthread_mutex_unlock(&amp;mut_main);
  
   return 0;
  }
  
  int launch_new_timer(pthread_t thread_id)
  {
   int rc;
   int seconds = GLOBAL_TIMER;
  
      rc =  pthread_create(&amp;thread_id, NULL, start_timer, (void *) &amp;seconds);
      if(rc)
       printf("Failed to create thread1\n");
  
      pthread_join(thread_id, NULL);
  
      return 0;
  }
  </code></pre>
  
  <hr>
  
  <h3>Clarification</h3>
  
  <p>I clarify here the real context of my code:
  I receive from the network some types of different protocols packets(ICMP, SSH, RIP, OSPF, BGP...), and i want to:</p>
  
  <ol>
  <li><p>identify every type of packets, let say with : identify_protocol(char *received_buffer), I got this function, it's ok.</p></li>
  <li><p>Check if i receive this type of protocols in the last 8 hours (THE TIMER OF EACH PROTOCOL TYPE EXPIRE AFTER 8 HOURS), two choices:</p></li>
  </ol>
  
  <p>a. if so, I dump the result data into a specific file on the disk.</p>
  
  <p>b. no, I didn't receive this type in the last 8 HOURS i create a new thread (in my code i simplify, with thread1, thread2 and thread3, this threads are 3 threads used to be a timers for three protocols types) - i start a new timer with : start_timer(void *args) function do this job.</p>
  
  <p>My main question is how to be able to check the result of my timers in a safe manner and then decide i reset the timer or not.</p>
  
  <p>I design the finish_function at the beginning to help me to check when the timer has expired.</p>
  
  <p>Feel free to give me a better design for best performances for my program.</p>
  
  <p>My system is Linux.</p>