c ++选择异步编程 [英] c++ select async programming
本文介绍了c ++选择异步编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一种方法可以选择等待读取和写入,同时还能够添加新的文件描述符?
Is there a way to have 'select' waiting for reads and writes, while also being able to add new file descriptors? Preferrably on one thread?
推荐答案
现在我知道你的场景是什么(一个基于套接字的服务器,可能想接受新的传入连接),你知道你可以附加你的侦听套接字的文件描述符到 select
的列表中?参见例如 http://www.lowtek.com/sockets/select.html 。
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.
(注释例子)
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, ...);
这篇关于c ++选择异步编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文