title: setjmp/longjmp between threads to handle timeout
body: <p>I'm porting a software from an embedded computer to a Linux machine. (Ubuntu 14.04 or Raspbian (raspberry pi))</p>
  
  <p>The original program was using setjmp/longjmp to handle timeout and CTRL+C event. It was running on a Microcontroller with a single main (one thread).</p>
  
  <p>I'm trying to have a similar behaviour while using threads (pthreads).</p>
  
  <p>The idea is that I want either a timeout or a CTRL+C to restart an infinite loop.</p>
  
  <p>The original code was doing something like the code below. I don't mind to drop the setjmp/longjmp by something else.  (ex: try/catch or signal or pthread_kill, conditional variable, etc..)</p>
  
  <p>Any idea how to implement similar behavior with C/C++ ?</p>
  
  <p>Here is the code which seems to partially work and is probably not recommended/broken:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;setjmp.h&gt;
  
  // Define
  #define TICK_NS_TIME (10000000)                                                 // 0.01 sec = 10 ms (100 times per second)
  #define NS_PER_SEC   (1000000000)                                               // Nano sec per second.
  #define TICK_PER_SEC (NS_PER_SEC/TICK_NS_TIME)                                  // Number of tick per second (Ex:100)
  #define TIMEOUT_COUNT (30*TICK_PER_SEC)                                         // 30 seconds timeout (with 100 tick per second)
  
  // Env set/long jmp
  #define ENV_SZ (2)
  #define ENV_TIMEOUT (0)
  #define ENV_CTRLC (1)
  static jmp_buf env[ENV_SZ];
  
  // Variables
  int timeout_val;
  
  
  // sig handler.
  void signal_handler(int signo)
  {
      pthread_t self = pthread_self();
      printf("Thread %lu in signal handler\n", (long)self);
      if (signo == SIGINT) {
          longjmp(env[ENV_CTRLC], 1);                                             // Q?: Is it in the same thread ? (Never, Always, Sometimes?)
      }
      else 
      {
          printf("Other signal received..quitting.");                             // Ex: kill -9 pid
          exit(0);
      }
  }
  
  // thread timer function
  void* timer_function(void* in_param)
  {
      // Loop approx 100x per second.
      for (;;) {
          nanosleep((const struct timespec[]){{0, TICK_NS_TIME }}, NULL);         // Sleep 10 ms seconds.
          if (timeout_val) {
              if (!--timeout_val) {
                  longjmp(env[ENV_TIMEOUT], 1);                                   // longjmp when timer reaches 0. (Q?: Is this valid with multithread?)
              }
          }
      }
  }
  
  // main
  int main(int argc, char **argv)
  {
      int i;
      int val;
      struct sigaction actions;
      pthread_t thread;
      setvbuf (stdout, NULL, _IONBF, 0);                                          // Make sure stdout is not buffered (ex:printf, etc.)
      printf("[Program started]\r\n");
  
      memset(&amp;actions, 0, sizeof(actions));
      sigemptyset(&amp;actions.sa_mask);
      actions.sa_flags = 0;
      actions.sa_handler = signal_handler;
      val = sigaction(SIGINT, &amp;actions, NULL);  
      pthread_create(&amp;thread, NULL, timer_function, NULL);                        // timer thread for example
      printf("[Timer thread started]\r\n");
  
      // setting env.
      val = setjmp(env[ENV_TIMEOUT]);
      if (val!=0){ printf("[JMP TIMEOUT]\r\n"); }
  
      val = setjmp(env[ENV_CTRLC]);
      if (val!=0){ printf("[JMP CTRLC]\r\n"); }
  
      // main loop
      timeout_val = TIMEOUT_COUNT;
      i = 0;
      for (;;)
      {
          i++;
          if (i &gt; 10){ i = 0; printf("[%d]", timeout_val/TICK_PER_SEC); }         // Number of seconds before time out.
          sleep(1);
          printf(".");
      }
      printf("Main completed\n");
      return 0;
  }
  //Compile: g++ -pthread main.cpp -o main
  </code></pre>
  
  <p>Suggestion for alternative implementation would be great since I'm new to programming with threads !</p>