title: processing pthread for clean exit
body: <p>linux gcc c89</p>
  
  <p>Currently I have an event loop that will capture and process events. This event loop will run in its own thread that is created from the main function. For testing purposes I am using a usleep in this loop.</p>
  
  <p>I have a condition app_running to control the loop and exit the loop.</p>
  
  <p>However, when I run my application I don't want to exit main because that will terminate the application. So I have a put a getchar() to wait for an input to indicate that I want to terminate the application. That will set the app_running to false to exit the event loop. This all looks a bit cheap. Is there a better way to do this without using the getchar()?</p>
  
  <p>Many thanks for any suggestions,</p>
  
  <p>Header</p>
  
  <pre><code>#ifndef NETWORK_TASKS_H_INCLUDED
  #define NETWORK_TASKS_H_INCLUDED
  
  #ifndef FALSE
  #define FALSE 0
  #endif
  #ifndef TRUE
  #define TRUE (!FALSE)
  #endif
  
  int app_running;
  
  void* process_events(void);
  
  #endif /* NETWORK_TASKS_H_INCLUDED */
  </code></pre>
  
  <p>Implemenation</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  
  #include "network_tasks.h"
  
  void* process_events(void)
  {
      app_running = TRUE;
  
      while(app_running) {
  #define TIMEOUT 3000000
          /* This will be used for capturing events. use usleep for simulating testing */
          /* if(net_events(TIMEOUT) != 0) { */
          /*     process_network_event(); */
          /* } */
          /* Just for testing */
          usleep(TIMEOUT);
          printf("Sleeping.....\n");
      }
  
      printf("Finished sleeping....\n");
  
      return NULL;
  }
  </code></pre>
  
  <p>main</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  
  #include "network_tasks.h"
  
  int main(void)
  {
      pthread_t th_id = 0;
      int th_rc = 0;
  
      th_rc = pthread_create(&amp;th_id, NULL, (void*)process_events, NULL);
  
      if(th_rc == -1) {
          fprintf(stderr, "Cannot create thread [ %s ]\n", strerror(errno));
          return -1;
      }
  
      getchar();
      app_running = FALSE;
  
      pthread_exit(NULL);
  
      return 0;
  }
  </code></pre>