void* worker(void *arg) { int sockfd = ((struct fds *)arg)->sockfd; int epollfd = ((struct fds *)arg)->epollfd; printf("start new thread to receive data on fd: %d\n", sockfd); char buf[BUFFER_SIZE]; memset(buf, '\0', BUFFER_SIZE); while(1) { int ret = recv(sockfd, buf, BUFFER_SIZE-1, 0); if (ret == 0) { close(sockfd); printf("foreiner closed the connection\n"); break; } else if (ret < 0) { if (errno == EAGAIN) { reset_oneshot(epollfd, sockfd); printf("read later\n"); break; } } else { printf("get content: %s\n", buf); sleep(5); } } printf("end thread recviving data on fd: %d\n", sockfd); }
void* worker( void* arg ) { int sockfd = ( (fds*)arg )->sockfd; int epollfd = ( (fds*)arg )->epollfd; //pthread_mutex_t mutex = ( (fds*)arg)->mutex; //pthread_mutex_lock(&mutex); printf( "start new thread to receive data on fd: %d\n", sockfd ); char buf[ BUFFER_SIZE ]; memset( buf, '\0', BUFFER_SIZE ); while( 1 ) { int ret = recv( sockfd, buf, BUFFER_SIZE-1, 0 ); if( ret == 0 ) { close( sockfd ); printf( "foreiner closed the connection\n" ); break; } else if( ret < 0 ) { if( errno == EAGAIN || errno == EWOULDBLOCK ) { /* read完后重置EPOLLONESHOT属性 */ reset_oneshot( epollfd, sockfd ); //pthread_mutex_unlock(&mutex); printf( "read later\n" ); break; } } else { printf( "get content: %s\n", buf ); sleep( 5 ); /* EPOLLONESHOT对socked文件描述符是独占的 * 一旦业务逻辑处理事件过长将会导致不会响应下一次read事件 * 导致数据丢失 * 所以这种方法是不可取的 */ } } printf( "end thread receiving data on fd: %d\n", sockfd ); return NULL; }