Giter Site home page Giter Site logo

Asyn_scope query about libunifex HOT 1 CLOSED

abhilashraju avatar abhilashraju commented on June 2, 2024
Asyn_scope query

from libunifex.

Comments (1)

janondrusek avatar janondrusek commented on June 2, 2024

Updated your example: https://godbolt.org/z/8eheK6Kz7

Looks like -fconcepts is not playing well with the library.

#include <iostream>
#include <utility>

#include <unifex/async_scope.hpp>
#include <unifex/inline_scheduler.hpp>
#include <unifex/just.hpp>
#include <unifex/just_error.hpp>
#include <unifex/just_from.hpp>
#include <unifex/let_error.hpp>
#include <unifex/let_value.hpp>
#include <unifex/static_thread_pool.hpp>
#include <unifex/sync_wait.hpp>
#include <unifex/then.hpp>
#include <unifex/upon_error.hpp>
#include <unifex/when_all.hpp>

using namespace unifex;

template <typename Handler>
struct on_error {
    Handler h;
    on_error(Handler h) : h(std::move(h)) {}

    template <typename Sender>
    friend auto operator|(Sender&& sender, on_error error) {
        return sender | let_error([](auto v) { return just_error(v); }) |
               upon_error([error = std::move(error)](auto exptr) {
                   try {
                       rethrow_exception(exptr);
                   } catch (std::runtime_error e) {
                       std::cout << e.what() << "\n";
                   }
                   error.h();
               });
    }
};

int main() {
    auto add_42 = then(just_from([]() noexcept {
                           std::cout << "Hello world! Have an int.\n";
                           return 13;
                       }),
                       [](auto&& arg) noexcept { std::cout << arg + 42 << "\n"; });
    auto leterror = add_42 | on_error([]() { std::cout << "hello"; });

    async_scope scope;
    scope.spawn(std::move(leterror));
    sync_wait(scope.complete());
    // auto(i) = sync_wait(std::move(leterror)).value(); // 5
}

/*
Output:
ASM generation compiler returned: 0
Execution build compiler returned: 0
Program returned: 0
Hello world! Have an int.
55
*/

from libunifex.

Related Issues (20)

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.