title: Wake up thread blocked on accept() call
body: <p>Sockets on Linux question</p>
  
  <p>I have a worker thread that is blocked on an accept() call.  It simply waits for an incoming network connection, handles it, and then returns to listening for the next connection.</p>
  
  <p>When it is time for the program to exit, how do I signal this network worker thread (from the main thread) to return from the accept() call while still being able to gracefully exit its loop and handle its cleanup code.</p>
  
  <p>Some things I tried:</p>
  
  <ol>
  <li><p>pthread_kill to send a signal.  Feels kludgy to do this, plus it doesn't reliably allow the thread to do it's shutdown logic.  Also makes the program terminate as well.  I'd like to avoid signals if at all possible.</p></li>
  <li><p>pthread_cancel.  Same as above.  It's a harsh kill on the thread.  That, and the thread may be doing something else.</p></li>
  <li><p>Closing the listen socket from the main thread in order to make accept() abort.  This doesn't reliably work.</p></li>
  </ol>
  
  <p>Some constraints:</p>
  
  <p>If the solution involves making the listen socket non-blocking, that is fine.  But I don't want to accept a solution that involves the thread waking up via a select call every few seconds to check the exit condition.</p>
  
  <p>The thread condition to exit may not be tied to the process exiting.</p>
  
  <p>Essentially, the logic I am going for looks like this.</p>
  
  <pre><code>void* WorkerThread(void* args)
  {
      DoSomeImportantInitialization();  // initialize listen socket and some thread specific stuff
  
      while (HasExitConditionBeenSet()==false)
      {
          listensize = sizeof(listenaddr);
          int sock = accept(listensocket, &amp;listenaddr, &amp;listensize);
  
          // check if exit condition has been set using thread safe semantics
          if (HasExitConditionBeenSet())
          {
              break;
          }
  
          if (sock &lt; 0)
          {
              printf("accept returned %d (errno==%d)\n", sock, errno);
          }
          else
          {
              HandleNewNetworkCondition(sock, &amp;listenaddr);
          }
      }
  
      DoSomeImportantCleanup(); // close listen socket, close connections, cleanup etc..
      return NULL;
  }
  
  void SignalHandler(int sig)
  {
      printf("Caught CTRL-C\n");
  }
  
  void NotifyWorkerThreadToExit(pthread_t thread_handle)
  {
      // signal thread to exit
  }
  
  int main()
  {
      void* ptr_ret= NULL;
      pthread_t workerthread_handle = 0;
  
      pthread_create(&amp;workerthread, NULL, WorkerThread, NULL);
  
      signal(SIGINT, SignalHandler);
  
      sleep((unsigned int)-1); // sleep until the user hits ctrl-c
  
      printf("Returned from sleep call...\n");
  
      SetThreadExitCondition(); // sets global variable with barrier that worker thread checks on
  
      // this is the function I'm stalled on writing
      NotifyWorkerThreadToExit(workerthread_handle);
  
      // wait for thread to exit cleanly
      pthread_join(workerthread_handle, &amp;ptr_ret);
  
      DoProcessCleanupStuff();
  
  }
  </code></pre>