Fundamentals 10 min read

Concurrency and I/O Models in Linux: Threads, Thread Pools, Coroutines, epoll, and AIO

This article explains common concurrency models, thread‑pool and coroutine techniques, and various Linux I/O models—including blocking, non‑blocking, multiplexing, and asynchronous I/O—highlighting their trade‑offs and practical usage in high‑performance backend systems.

Architects' Tech Alliance
Architects' Tech Alliance
Architects' Tech Alliance
Concurrency and I/O Models in Linux: Threads, Thread Pools, Coroutines, epoll, and AIO

Common concurrency models include thread‑and‑lock shared memory, actor, and CSP; the thread‑and‑lock model is most common, while CSP gains attention with Go.

Concurrency aims to fully utilize multi‑core CPUs; correct operation requires synchronization via mutexes, condition variables, semaphores, or inter‑process communication such as shared memory, pipes, or sockets.

The simplest model is single‑process single‑thread, which is inefficient. Introducing a thread per request improves utilization but incurs high thread creation/destruction and context‑switch overhead.

Thread pools pre‑create a fixed number of reusable threads, reducing overhead. MySQL’s thread pool changes from one‑connection‑one‑thread to one‑statement‑one‑thread, improving reuse. Coroutines are user‑space lightweight threads that avoid kernel scheduling, offering cooperative multitasking with better readability than callbacks, though they cannot exploit multiple cores.

单线程-->(单线程轮询处理,太慢)
多线程-->(多线程会频繁地创建、销毁线程,这对系统也是个不小的开销。这个问题可以用线程池来解决。)
线程池-->(仍然有多线程上下文切换的问题,调度由内核调度)
协程-->(应用层调度,不touch内核)

I/O in Linux abstracts devices as files; typical I/O models are blocking, non‑blocking, I/O multiplexing, asynchronous non‑blocking, and asynchronous I/O.

Blocking I/O uses the raw read/write system calls and blocks the thread. Non‑blocking I/O sets the file descriptor to non‑blocking mode and returns an error if not ready, requiring the application to poll.

I/O multiplexing (select/poll/epoll) lets a single thread wait for events on many descriptors. epoll improves scalability with O(1) event handling and supports edge‑triggered mode, reducing system calls compared to select/poll.

Libraries such as libev/libeasy unify different event sources (fd, signal, timer) into a single reactor framework, allowing user‑defined callbacks for ready events.

Linux AIO originally simulated asynchronous I/O with worker threads (POSIX AIO). Native kernel AIO submits batches of I/O requests without blocking, offering higher parallelism. Common AIO interfaces include io_setup , io_destroy , io_submit , io_cacel , and io_getevents .

Comparison:

Synchronous I/O: simple but blocks and wastes CPU.

Native AIO: can issue multiple discontiguous requests, higher performance, but requires O_DIRECT support.

POSIX AIO: does not require O_DIRECT and can merge adjacent requests, but concurrency is limited by thread count.

Author: Yan, Alibaba Cloud PolarDB M kernel R&D expert, focusing on storage engine development.

Concurrencylinuxthread poolI/O modelsepollaio
Architects' Tech Alliance
Written by

Architects' Tech Alliance

Sharing project experiences, insights into cutting-edge architectures, focusing on cloud computing, microservices, big data, hyper-convergence, storage, data protection, artificial intelligence, industry practices and solutions.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.