网络编程模型(忙等,select,epoll) :
A. 忙等:(没有新消息到来的时候,死循环耗费CPU)
while true {
for i in stream[] {
if i has data
read until unavailable
}
}
B. select模型:(没新消息就block在select那里,有新消息就往下执行;不知道哪个信道的消息到了,要所有信道轮询一遍)
while true {
select(streams[])
for i in streams[] {
if i has data
read until unavailable
}
}
C. epoll模型:(没新消息就block在epoll那里,有新消息就往下执行,返回值会告诉哪些信道有新消息,直接操作哪些信道即可)
epoll+线程池,把实际read/write的时间并行掉,同时减少线程创建/销毁时间开销,才是正解。
while true {
active_stream[] = epoll_wait(epollfd)
for i in active_stream[] {
read or write until unavailable
}
}