patrykstefanski / libfev

A library for events and fibers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

libfev

A library for events and fibers (a.k.a. green threads/goroutines/stackful coroutines).

CI

Overview

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)

Performance

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.

Support

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

Example

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.

Documentation

License

Licensed under either of

at your option.

Third party

This library includes some code written by third parties. Check third_party for their licenses.

Contribution

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.

About

A library for events and fibers

License:Apache License 2.0


Languages

Language:C 81.8%Language:C++ 12.0%Language:CMake 5.0%Language:Assembly 1.2%Language:Objective-C 0.1%