Cette page est affichée en anglais. Une traduction française est en cours.
ScriptsMay 26, 2026·3 min de lecture

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.

Prêt pour agents

Installation agent prête

Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
libevent Overview
Commande d'installation directe
npx -y tokrepo@latest install cdf0e4d1-5898-11f1-9bc6-00163e2b0d79 --target codex

À exécuter après confirmation du plan en dry-run.

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

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires