SamGaaWaa / asio2exec

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A single header. Adapt ASIO to P2300 execution

Usage:

  • namespace asio2exec
  • asio_context
  • completion token use_sender makes asynchronous functions return a sender

Example:

#include "stdexec/execution.hpp"
#include "asio2exec.hpp"
#include "asio/steady_timer.hpp"
#include <iostream>

namespace ex = stdexec;

int main() {
    asio2exec::asio_context ctx;
    asio::steady_timer timer{ctx.get_executor(), std::chrono::seconds(3)};

    ctx.start();

    ex::sender auto work = timer.async_wait(asio2exec::use_sender) |
                            ex::then([](asio::error_code ec){
                                if(ec)
                                    throw asio::system_error{ec};
                                std::cout << "Hello World\n";
                            });
    
    ex::sync_wait(std::move(work));
}

Configure:

To use Boost.Asio, define ASIO_TO_EXEC_USE_BOOST

Note: The io operations of asio's io objects(timer, socket) are always performed in the context which used to construct the io object, but subsequent operations are guaranteed at the correct scheduler.

asio2exec::asio_context ctx1;
asio2exec::asio_context ctx2;
ctx1.start();
ctx2.start();

asio::steady_timer timer{ctx1.get_executor(), std::chrono::seconds(3)};

auto work = ex::schedule(ctx2.get_scheduler()) |
            ex::let_value([&]{
                return timer.async_wait(asio2exec::use_sender); // timer waiting in ctx1
            }) |
            ex::then([](asio::error_code ec){
               // in ctx2 
            });

About

License:MIT License


Languages

Language:C++ 99.6%Language:Makefile 0.4%Language:CMake 0.0%