> For the complete documentation index, see [llms.txt](https://aceld.gitbook.io/libevent/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://aceld.gitbook.io/libevent/2-epoll/21-jie-jue-zu-sai-si-deng-dai-de-ban-fa.md).

# 2.2 解决阻塞死等待的办法

## 2.2.1 阻塞死等待的缺点

![](/files/-Lce3rWq9HiVxEej8lGt)

## 2.2.2 办法一：非阻塞、忙轮询

![](/files/-Lce3rWsAZBf6hslLPJS)

```cpp
while true {
    for i in 流[] {
        if i has 数据 {
            读 或者 其他处理
        }
    }
}
```

## 2.2.3 办法二：select

![](/files/-Lce3rWustv4CJgOYojx)

select 代收员 比较懒，她只会告诉你快递到了，但是是谁到的，你需要挨个快递员问一遍。

```cpp
while true {
    select(流[]);  //阻塞

    for i in 流[] {
        if i has 数据 {
            读 或者 其他处理
        }
    }
}
```

## 2.2.3 办法三：epoll

![](/files/-Lce3rWwyPH8OAJt7-ae)

```cpp
while true {
    可处理的流[] = epoll_wait(epoll_fd);  //阻塞

    for i in 可处理的流[] {
        读 或者 其他处理
   }
}
```
