coyorkdow / cfuture

a c++ promise future implementation with continuation [work in progress]

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cfuture

A promise future implementation with continuation.

cfuture is header-only, and it only has a single file cfuture.hpp. It requires cpp14.

Motivation

cfuture is aimed to provide a promise/future class which supports future::then to attach the continuations like a chain. I admit that async/await is a better practice than promise/future when writing the asynchronous codes. But there are still a lot of projects cannot use c++20 or higher standard. Or, sometimes, the promise/future is easier than coroutine.

The boost::future and std::experimental::future (in concurrency TS) both support then. But I hope to provide a light and easy-to-rely implementation. It isn't the cfuture's goal to be completely complied with the design of std::future or boost::future. But still, it will very close to them. It could be an alternate in most cases.

Quick Start

  using namespace std::chrono_literals;
  using namespace cfuture;
  Promise<int> p;
  std::cout << "the main thread is " << std::this_thread::get_id() << '\n';
  std::thread th([&] {
    std::cout << "run in thread " << std::this_thread::get_id() << '\n';
    std::this_thread::sleep_for(100ms);
    p.set_value(1);
  });
  Future<std::string> f;
  f = p.get_future()
          .then([](Future<int> v) {
            Promise<int> p;
            auto future = p.get_future();
            std::thread th([v = v.get(), p = std::move(p)]() mutable {
              std::cout << "run in thread " << std::this_thread::get_id() << '\n';
              std::this_thread::sleep_for(100ms);
              p.set_value(v + 2);
            });
            th.detach();
            return future;
          })
          .then([](Future<int> v) -> long {
            std::cout << "run in thread " << std::this_thread::get_id() << '\n';
            std::this_thread::sleep_for(1ms);
            return v.get() + 3;
          })
          .then([](Future<long> v) {
            std::cout << "run in thread " << std::this_thread::get_id() << '\n';
            std::this_thread::sleep_for(1ms);
            return std::to_string(v.get() + 4);
          });
  std::cout << f.get() << '\n';
  th.join();

The possible output of the above codes is

the main thread is 0x7ff857913340
run in thread 0x70000da84000
run in thread 0x70000db07000
run in thread 0x70000db07000
run in thread 0x70000db07000
10

About

a c++ promise future implementation with continuation [work in progress]

License:MIT License


Languages

Language:C++ 96.4%Language:CMake 3.6%