c - Select() doesn't recognise changes through FD_SET while blocking -
i'm calling fd_set() set write_fd of non-blocking socket while select() blocking within thread – problem select() keeps blocking if fd ready write.
what want is: preparing data write socket within thread, afterwards add socket write_fd. select() thread should recognise , handle prepared data.
doesn't select() recognise changes within fd while blocking? if yes – there epoll() epoll_ctl_mod instead of fd_set() update set; or way recognise changes set timeout of select()-function?
in opinion wouldn't solution because "slow" , produce cpu overhead ...
edit:
// thread running day long ... static void * workman() { fd_zero(&fd_read); fd_zero(&fd_write); fd_set(socketid , &fd_read); while(1) { // problem keeps blocking when box() called select(socketid+1, &fd_read, &fd_write, null, null); if(fd_isset(socketid, &fd_read)) { // recive data } else if(fd_isset(socketid, &fd_write)) { fd_clr(socketid, &fd_write); pthread_mutex_lock(&interface.mutex); strncpy(conn.outbuffer, interface.buffer, strlen(interface.buffer)); interface.buffer[0] = '\0'; pthread_mutex_unlock(&interface.mutex); // send data } } return 0; } // function called within thread on user input int box(char *content) { pthread_mutex_lock(&interface.mutex); // preparing data , write interface.buffer if available pthread_mutex_unlock(&interface.mutex); fd_set(socketid, &fd_write); return 0; }
yes, suspect, select()
not detect changes done file descriptor sets thread. after all, cannot efficiently, without magic mechanism asynchronously detect writes specific memory locations.
and, yes, should use epoll
interface. man page of epoll_wait
notes changes thread handled.
while 1 thread blocked in call epoll_pwait(), possible thread add file descriptor waited-upon epoll instance. if new file descriptor becomes ready, cause epoll_wait() call unblock.
but, if unable use epoll
or file notification interface supports such changes, there still solution. can use internal pipe
(or similar mechanisms such eventfd
), cause select
return , restart after have updated file descriptor sets. of course, should careful proper locking avoid race conditions. likewise, sure put pipe non-blocking mode, or write
write end of pipe block under heavy load , possibly cause program deadlock.
Comments
Post a Comment