According to the information "a person chooses":
"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."
Choose wakup because of:
1)read/write availability 2)select error 3)descriptoris closed.
However, how can we wake select () from another thread if there is no data available and the choice is still in timeout?
[update]
Pseudo code
// Thread blocks on Select void *SocketReadThread(void *param){ ... while(!(ReadThread*)param->ExitThread()) { struct timeval timeout; timeout.tv_sec = 60; //one minute timeout.tv_usec = 0; fd_set rds; FD_ZERO(&rds); FD_SET(sockfd, &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, &rds, NULL, NULL, &timeout ); //handle the result //might break from here } return NULL; } //main Thread int main(){ //create the SocketReadThread ReaderThread* rthread = new ReaderThread; pthread_create(&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->SetExitFlag(true); //but how to wake up select ?????????????????? //if SocketReaderThread currently blocks on select }
[UPDATE]
1) @trojanfoe provides a method to achieve this; its method writes socket data (possibly dirty data or message exit data) to select wakeup. I am going to pass the test and update the result.
2) Another thing worth mentioning is that closing a socket does not guarantee waking up a function call, see this post .
[UPDATE2]
After many tests, here are some facts about waking select:
1) If the socket viewed by select is closed by another application , then select () the call will wake up immediately. Further reading or writing to the socket will receive a return value of 0 with errno = 0
2) If the socket viewed by select is closed by another thread of the same application , then select () will not wake up before the timeout if there is no data to read or write. After selecting timeouts, performing a read / write operation results in an error with errno = EBADF (since the socket was closed by another thread during the wait period)