title: how to wakeup select() within timeout from another thread
body: <p>According to the "man select" information:              </p>
  
  <pre><code>     "On  success,  select() and pselect() return the number of file descrip‐
     tors contained in the three returned  descriptor  sets which may be zero 
     if the timeout expires  before  anything  interesting happens.  On error, 
     -1 is returned, and errno is set appropriately; the sets and timeout become
     undefined, so do not  rely  on  their  contents after an error."
  </code></pre>
  
  <p>Select will wakup because of:    </p>
  
  <pre><code>       1)read/write availability       
         2)select error                 
         3)descriptoris closed.  
  </code></pre>
  
  <p>However, how can we wake up the select() from another thread if there is no data available and the select is still within timeout? </p>
  
  <p><strong>[update]</strong><br>
  Pseudo Code</p>
  
  <pre><code>          // Thread blocks on Select
            void *SocketReadThread(void *param){
                 ...
                 while(!(ReadThread*)param-&gt;ExitThread()) {
                     struct timeval timeout;
                     timeout.tv_sec = 60; //one minute
                     timeout.tv_usec = 0;
  
                     fd_set rds;
                     FD_ZERO(&amp;rds);
                     FD_SET(sockfd, &amp;rds)'
  
                     //actually, the first parameter of select() is 
                      //ignored on windows, though on linux this parameter
                     //should be (maximum socket value + 1)
                     int ret = select(sockfd + 1, &amp;rds, NULL, NULL, &amp;timeout );
                     //handle the result
                     //might break from here
  
                 }
                 return NULL;
            }
  
            //main Thread
            int main(){
                  //create the SocketReadThread
                  ReaderThread* rthread = new ReaderThread;
                  pthread_create(&amp;pthreadid, NULL, SocketReaderThread, 
                            NULL, (void*)rthread);
  
                   // do lots of things here
                   ............................
  
                  //now main thread wants to exit SocketReaderThread
                  //it sets the internal state of ReadThread as true
                  rthread-&gt;SetExitFlag(true);
                  //but how to wake up select ??????????????????
                  //if SocketReaderThread currently blocks on select
            }
  </code></pre>
  
  <p><strong>[UPDATE]</strong><br>
  1) @trojanfoe provides a method to achieve this, his method writes socket data (maybe dirty data or exit message data) to wakeup select. I am going to have a test and update the result there.<br>
  2) Another thing to mention, closing a socket doesn't guarantee to wake up select function call, please see <strong><a href="https://stackoverflow.com/questions/16965053/shutdown-wakes-up-select-but-errno-remains-0-no-ebadf">this post</a></strong>.</p>
  
  <p><strong>[UPDATE2]</strong><br>
  After doing many tests, here are some facts about waking up select:<br>
  1) If the socket watched by select is closed <strong>by another application</strong>, then select() calling
  will wakeup immediately. Hereafter, reading from or writing to the socket will get return value of 0 with an errno = 0<br>
  2) If the socket watched by select is closed by <strong>another thread of the same application</strong>,
  then select() won't wake up until timeout if there is no data to read or write. After select timeouts, making read/write operation results in an error with errno = EBADF 
  (because the socket has been closed by another thread during timeout period)              </p>