简体   繁体   中英

Pipeline dataflow using C++11 async features

I'm trying to implement a multithreaded pipeline dataflow framework with the following features:

  1. The pipeline can be described as an acyclic directed graph. Each node performs some processing and has arbitrary number of inputs of arbitrary types and one output of arbitrary type.

  2. For each given input data instance each node should be executed not more that once, after that the result should be cached. Although this cache shouldn't persist in memory longer that required and should be deleted when no longer needed by any other nodes.

  3. Each node should support lazy evaluation, ie should be executed only at the moment it's output is needed by some another node.

Is it possible to implement this by using C++11 multithreading features, especially std::future , std::promise and std::async ? Can anybody give a clue?

I believe it actually is fairly trivial using an async framework.

If you look at std::launch you will realize there is a deferred mode:

  • std::launch::deferred : the task is executed on the calling thread the first time its result is requested (lazy evaluation)

Thus, you can launch a task and have it executed only when the result is needed. However, since you mention an acyclic graph, you probably want to share the result: a std::future (as returned by a call to std::async ) cannot be shared; you need a std::shared_future for this.

And thus, putting it altogether:

// Disclaimer:
// Compiles but does not run, but I have not figured it out.
// See: http://ideone.com/XZ49Dg

#include <future>
#include <iostream>

int main() {
    std::shared_future<std::string> greeting = std::async(std::launch::deferred, []() {
        std::string s;
        std::cout << "Enter how you would like to be greeted: ";
        std::getline(std::cin, s);
        return s;
    });

    std::shared_future<int> choice = std::async(std::launch::deferred, []() {
        int c = 0;
        std::cout << "Pick any integer: ";
        std::cin >> c;
        return c;
    });

    std::shared_future<void> complete = std::async(std::launch::deferred, [=]() mutable {
        std::string const g = greeting.get();
        int const c = choice.get();

        std::cout << "Hello " << g << ", you picked " << c << "!\n";
    });

    complete.wait();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM