A library for events and fibers
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:
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:
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
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.