Giter VIP home page Giter VIP logo

rio's Introduction

Rio License Badge PR Badge

A runtime for writing slim, concurrent, and asynchronous applications with C++20.

Key Features

  • Performant: Rio's near zero-cost abstractions enable bare-metal performance.
  • Flexible: Supports multiple task types and scheduling policies.
  • Scalable: Rio tightly manages resources and minimizes busy spinning.

Getting Started

Task Submission

#include "rio/executor.hpp"

int make_http_request(std::string_view body) {
  // ...
}

int main() {
  // Initialize the runtime by creating an executor object.
  rio::executor executor;
  auto& scheduler = executor.get_scheduler();

  // a. Submit an asynchronous task which produces an integer to the executor.
  std::future<int> f1 = scheduler.await([](int n) { return 2 * n; }, 10);
  std::cout << f1.get() << '\n';
  //              ^^^
  //              Retrieve the result via the generated std::future.

  // b. Submit an asynchronous void task to the executor.
  scheduler.await([](int n) { std::cout << n << '\n'; }, 10);

  // c. Submit a task via a function pointer to the executor.
  std::future<int> f2 = scheduler.await(make_http_request, "...");
  std::cout << f2.get() << '\n';
  //              ^^^
  //              Similarly retrieve the result via the generated std::future.
}

Custom Schedulers & Pool Sizes

#include "rio/executor.hpp"
#include "rio/scheduler.hpp"

// Define a custom scheduling policy and task scheduler by implementing the
// rio::scheduler interface.
class custom_scheduler : public rio::scheduler {
  // ...
};

int main() {
  rio::executor<8, custom_scheduler> executor;
  //            ^^^^^^^^^^^^^^^^^^^
  //            Use a custom pool size and scheduler implementation.
  auto& scheduler = executor.get_scheduler();

  // Submit a void task to the executor. Since execution is guaranteed to
  // occur strictly once, std::cout synchronization is not required in this
  // trivial case.
  scheduler.await([]() { std::cout << "Hello World\n"; });
}

Example: Reading Files

#include "rio/executor.hpp"

void append_to_file(std::filesystem::path path, std::string_view content) {
  // ...
}

int main() {
  // Initialize the runtime by creating an executor object.
  rio::executor executor;
  auto& scheduler = executor.get_scheduler();

  // Get the file system path for all files to append to.
  std::vector<std::filesystem::path> paths = get_file_paths();

  // Asynchronously append content to all files in the paths vector. Optionally,
  // store all generated std::future objects to check for thrown exceptions.
  for (auto& path : paths) {
    scheduler.await(append_to_file, path, "๐Ÿ‘‹");
  }
}

rio's People

Contributors

ayushgun avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.