所以在我的当前服务器实现中,它目前是这样的:
void loop(){
// step 1: clear set
fd_set readfds;
while(true){
// step 1:
FD_ZERO(readfds);
// step 2:
loop_through_sockets_and_add_active_sockets_to(theset);
// step 3:
switch(select(FD_SETSIZE, &readfds, 0, 0, &tv)) {
case SOCKET_ERROR:
patia->receiveEvent(Error, net::getError());
return;
case 0:
return;
}
// step 4:
loop through sockets and check, using FD_ISSET,
which read fd's have incoming data.
}
}
现在,不清除fd_set(只使用FD_SET、FD_CLR在添加/删除通道时)将是更好的做法。
我的问题是,如何在select()之后循环遍历fd_set,而不检查集合中的每个成员是否属于该集合,而又不使用FD_ISSET?
我的意思是,当您有4000个活动连接时,每当有传入数据时,上述循环将不得不在到达正确连接之前通过潜在的4000个套接字。如果所有线程都很活跃,复杂性将是n^2!