I don't have much experience with asynchronous I/O on sockets. Recently I had to implement a TCP client that would connect to a server in a seperate thread and wait for input data and that in a non-blocking manner so that the thread could be terminated immediately by simply setting some flag varaible from the control thread. My solution was setting socket non-blocking and implementing a loop that called recv over and over again if errno was E_WOULDBLOCK and exited loop when flag variable run wasn't set anymore. It was like that:
while(run) {
if( -1 == recv(...))
{
if(errno == E_WOULDBLOCK)
continue;
else
break;
}
else
{
process_data(...);
}
}
The question is: how good is this solution in terms of CPU usage? Will using select/poll/epoll be more effective?
And another one question: is there any way to make blocked select, read or connnect call return immediately from other thread?