title: C : non blocking sockets with timeout : how to avoid busy waiting?
body: <p>The following code allows for a server to wait for a client to connect to a (already binded) socket. 
  It terminates either when a client connects to the socket, or when "server_run" takes the value 0: this allows other part of the code to shutdown the server when suitable to do so.</p>
  
  <pre><code>static inline int wait_for_client_to_connect(int sockfd, int* server_run){                                                                                                                                                                                  
    int client_found = 0;                                                                                                                                                                                                                                        
    int clientfd = 0;                                                                                                                                                                                                                                            
    struct sockaddr_in client_addr;                                                                                                                                                                                                                              
    int addrlen=sizeof(client_addr);                                                                                                                                                                                                                             
    if ( listen(sockfd,1) != 0 ) return -1;                                                                                                                                                                                                                      
    while ( client_found==0 &amp;&amp; *server_run==1 ) {                                                                                                                                                                                                                 
      clientfd = accept(sockfd, (struct sockaddr*)&amp;client_addr, &amp;addrlen);                                                                                                                                                                                       
      if ( clientfd &lt; 0 ) {                                                                                                                                                                                                                                      
        clientfd = 0;                                                                                                                                                                                                                                            
        if (errno==EAGAIN || errno==EWOULDBLOCK) usleep(10); // nobody connected, wait for request                                                                                                                                                               
        else return -1; // something wrong, send error                                                                                                                                                                                                           
      } else { // client found, configuring socket and exit                                                                                                                                                                                                      
        client_found=1;                                                                                                                                                                                                                                          
        int nodelay_flag = 1;                                                                                                                                                                                                                                    
        setsockopt(clientfd, IPPROTO_TCP, TCP_NODELAY, (void*) &amp;nodelay_flag, sizeof(int)); // disable nagle algorithm                                                                                                                                           
      }                                                                                                                                                                                                                                                          
    }                                                                                                                                                                                                                                                            
    return clientfd;                                                                                                                                                                                                                                             
  }                    
  </code></pre>
  
  <p>According to answer and comments to another posts (<a href="https://stackoverflow.com/questions/28764440/c-non-blocking-sockets-with-timeout-how-to-check-if-connection-request-was-m">C : non blocking sockets with timeout : how to check if connection request was made?</a>), this is not the way to go because it involves busy waiting. </p>
  
  <p>E.g. a comment says :</p>
  
  <blockquote>
    <p>"The standard way to handle shutdown with blocking IO is to use a
    signal handler to set a shutdown flag and then check the flag when
    listen returns -1 with errno set to EINTR"</p>
  </blockquote>
  
  <p>It is very unclear to me how the code above could be adapted to "use a signal handler" ....</p>