A library for events and fibers (a.k.a. green threads/goroutines/stackful coroutines).
libfev is an abstraction over event-driven, non-blocking I/O for writing programs in C and C++ in a simple blocking style. It provides:
- Few multithreaded schedulers
- Backends for epoll and kqueue (and experimental io_uring backend)
- Timers
- Synchronization primitives (mutex, condition variable and semaphore)
In a throughput benchmark libfev can handle up to 172% more requests per second than Boost.Asio, up to 77% more than Tokio, up to 40% more than async-std and up to 16% more than Go. See async-bench for more data, there is also a comparison of the available schedulers.
Following platforms are currently supported:
- x86 (both 32- and 64-bit)
- FreeBSD, Linux, macOS
- DragonFlyBSD, NetBSD, OpenBSD should work too, but I haven't tested them yet
- Clang >= 9 or GCC >= 8
void echo(fev::socket &&socket) try {
char buffer[1024];
for (;;) {
std::size_t num_read = socket.read(buffer, sizeof(buffer));
if (num_read == 0)
break;
socket.write(buffer, num_read);
}
} catch (const std::system_error &e) {
std::cerr << "[echo] " << e.what() << '\n';
}
void acceptor() {
fev::socket socket;
socket.open(AF_INET, SOCK_STREAM, 0);
socket.set_reuse_addr();
socket.bind(reinterpret_cast<sockaddr *>(&server_addr), sizeof(server_addr));
socket.listen(1024);
for (;;) {
auto new_socket = socket.accept();
fev::fiber::spawn(&echo, std::move(new_socket));
}
}
See also examples.
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
This library includes some code written by third parties. Check third_party for their licenses.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.