> 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 可处理的流[] {
        读 或者 其他处理
   }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://aceld.gitbook.io/libevent/2-epoll/21-jie-jue-zu-sai-si-deng-dai-de-ban-fa.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
