# libevent — Event-Driven Network Programming Library > A portable C library for scalable asynchronous I/O, providing event notification across epoll, kqueue, IOCP, and more, used by Memcached, Tor, and Chromium. ## Install Save as a script file and run: # libevent — Event-Driven Network Programming Library ## Quick Use ```bash # Install sudo apt install libevent-dev # Debian/Ubuntu brew install libevent # macOS ``` ```c #include void timeout_cb(evutil_socket_t fd, short what, void *arg) { printf("Timeout fired!\n"); } int main() { struct event_base *base = event_base_new(); struct timeval tv = {2, 0}; // 2 seconds struct event *ev = evtimer_new(base, timeout_cb, NULL); evtimer_add(ev, &tv); event_base_dispatch(base); event_free(ev); event_base_free(base); return 0; } ``` ## Introduction libevent provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout. It is one of the oldest and most battle-tested event notification libraries in the C ecosystem, powering critical infrastructure like Memcached, Tor, and parts of Chromium. ## What libevent Does - Abstracts OS-specific I/O multiplexing (epoll, kqueue, IOCP, /dev/poll, select) behind one API - Provides buffered event I/O with automatic read/write watermarks (bufferevents) - Offers HTTP server and client implementations for embedding lightweight web services - Handles DNS resolution asynchronously without blocking the event loop - Supports OpenSSL-based encrypted connections integrated with the bufferevent layer ## Architecture Overview libevent centers on an `event_base` that manages a priority queue of pending events. When you add an event (I/O readiness, signal, or timer), the base registers it with the OS backend. The dispatch loop blocks until events fire, then invokes callbacks in priority order. Bufferevents layer read/write buffering on top of raw events, handling partial reads and flow control automatically. ## Self-Hosting & Configuration - Build with CMake or autoconf: `mkdir build && cd build && cmake .. && make` - Link against `libevent_core` (minimal), `libevent_extra` (HTTP, DNS), or `libevent_openssl` - Configure the backend explicitly or let libevent auto-select the fastest available - Set `EVENT_BASE_FLAG_NOLOCK` for single-threaded applications to avoid locking overhead - Works on Linux, macOS, Windows, FreeBSD, Solaris, and most POSIX systems ## Key Features - Proven at scale: Memcached serves millions of requests/sec using libevent - Bufferevent abstraction handles partial I/O, TLS handshakes, and rate limiting - Built-in HTTP 1.1 server suitable for embedding admin interfaces or health checks - Thread-safe with explicit locking or lock-free single-threaded mode - BSD licensed with over 20 years of production use ## Comparison with Similar Tools - **libuv** — newer, better Windows support, powers Node.js; libevent has richer built-in protocols - **libev** — faster on Unix but no Windows IOCP support and fewer built-in features - **Boost.Asio** — C++ only with heavy template usage; libevent is plain C - **io_uring (liburing)** — Linux-only, lower-level; libevent is cross-platform - **epoll/kqueue directly** — maximum control but platform-specific and more code ## FAQ **Q: Should I use libevent or libuv for a new project?** A: libuv has a more modern API and better Windows integration. libevent is better if you need the built-in HTTP server, DNS resolver, or OpenSSL bufferevent. **Q: Is libevent thread-safe?** A: Yes, when compiled with threading support. Each thread should have its own event_base, or you can use `event_base_loopbreak()` / `event_active()` to communicate across threads. **Q: Can I handle thousands of concurrent connections?** A: Yes. libevent with epoll or kqueue scales to hundreds of thousands of connections. Memcached regularly handles 100K+ concurrent connections per instance. **Q: Does libevent support HTTP/2?** A: The built-in HTTP module supports HTTP/1.1 only. For HTTP/2, pair libevent with nghttp2 or use a different HTTP stack. ## Sources - https://github.com/libevent/libevent - https://libevent.org/doc/ --- Source: https://tokrepo.com/en/workflows/asset-cdf0e4d1 Author: Script Depot