GithubHelp home page GithubHelp logo

await's Introduction

These projects explore the coroutine proposal as implemented in Visual Studio 2015 Update 2. It includes an async_generator<T> and some algorithms and adaptors to use it.

Async Operators Example

future<void> waitfor() {
    for co_await(auto v : fibonacci(10) | 
        copy_if([](int v) {return v % 2 != 0; }) |
        transform([](int i) {return to_string(i) + ","; }) | 
        delay(1s)
    ) {
        std::cout << v << ' ';
    }
}

int wmain() {
    waitfor().get();
}

This code is in the async project. This code looks similar to Eric Niebler's Range proposal (GitHub, Blog), but these are async ranges. Not only are the types involved different, but also the for loop and the algorithms. Coordinating many Ranges from many threads over time has additional complexity and different algorithms. The ReactiveExtensions family of libraries provide a lot of algorithms useful for async Ranges. The RxMarbles site has live diagrams for many of the algorithms. rxcpp implements some of these algorithms in C++ without await.

Values distributed in Time

This is a table of types that represent each combination of values and time.

| Value | Sequence --------|----------|------------- past | T | vector<T> lazy | []() -> T { . . . } | generator<T> later | future<T> | async_generator<T>

  • past - A value has already been produced before the caller asks for it.
  • lazy - A value is produced when asked for by a caller.
  • later - When a value is produced the caller is resumed.

The three that I explore in the context of await are future<T>, generator<T> and async_generator<T>.

future<T> - the value arrives Later

This is covered first because yield_value is composed on top of await. future<T> represents a value of T that may become available later. It may hold an exception instead. A function that returns future<T> is allowed to be called using co_await.

generator<T> - each value is Lazy

generator<T> implements the Range Concept so it works with existing algorithms from STL and Rangev3 and the range-for feature. It can be found in the header experimental/generator. A function that returns generator<T> is allowed to use the co_yield keyword (which evaluates to co_await generator<T>::promise_type::yield_value(T)).

async_generator<T> - each value arrives Later

async_generator<T> implements a new AsyncRange Concept. begin() and ++iterator return an awaitable type that produces an iterator later while end() returns an iterator immediately. A new set of algorithms is needed and a new async-range-for has been added that inserts co_await into i = co_await begin() and co_await ++i. A function that returns async_generator<T> is allowed to use co_await and co_yield.

Resources

  • Gor Nishanov kindly answered many emails as I worked on the code. His epic presentation (PDF, YouTube) of the design, implementation and sample usage at CPPCON 2014 is required watching.
  • James McNellis made a great presentation for MeetingC++ PDF, YouTube

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.