开发者

c++ select async programming

开发者 https://www.devze.com 2023-02-24 02:02 出处:网络
Is there a way to have \'select\' waiting 开发者_JAVA百科for reads and writes, while also being able to add new file descriptors? Preferrably on one thread?Now that I know what your scenario is (a soc

Is there a way to have 'select' waiting 开发者_JAVA百科for reads and writes, while also being able to add new file descriptors? Preferrably on one thread?


Now that I know what your scenario is (a socket-based server that may want to accept new incoming connections), did you know that you can append the file-descriptor for your listening socket to the list for select? See e.g. http://www.lowtek.com/sockets/select.html.

(Paraphrased example:)

fd_set socks;

FD_ZERO(&socks);

// Add listener socket
listen(sock, n);
FD_SET(&socks, sock);

// Add existing socket connections
for (i = 0; i < num_existing_connections; i++)
{
    FD_SET(&socks, connection[i]);
}

// Will break if any of the existing connections are active,
// or if a new connection appears.
select(..., &socks, ...);


As far as I think, You can do it in same thread but not at the same time. In a problem like this I normally add my dummy loop-back socket in the descriptor list and whenever I have to add a new socket in FD_LIST, I just send a byte to my dummy socket and it breaks the Select Loop. Then I can update the FD_LIST and resume with the select again.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号