Simple Future examples.
#include <chrono>
#include <exception>
#include <iostream>
#include <string>
#include <type_traits>
#include <utility>
#include <gtest/gtest.h>
using namespace std::chrono_literals;
TEST(Example, HelloWorld) {
std::move(p).Set(42);
EXPECT_TRUE(f.Ready());
EXPECT_EQ(std::move(result).Ok(), 42);
}
return 42;
});
std::cout << "Async result from thread pool: " << std::move(result).Ok() << std::endl;
});
EXPECT_TRUE(!f.Valid());
tp.SoftStop();
tp.Wait();
}
auto compute = [] {
return 42;
};
auto process = [](int r) {
return r + 1;
};
EXPECT_TRUE(!f1.
Valid());
std::cout << "process(compute()) -> " << std::move(f2).Get().Ok() << std::endl;
tp.SoftStop();
tp.Wait();
}
TEST(Example, Pipeline) {
auto first = []() -> int {
return 42;
};
auto second = [](int r) {
return r * 2;
};
auto third = [](int r) {
return r + 1;
};
auto fourth = [](int r) {
return std::to_string(r);
};
std::cout << "Pipeline result: <" << std::move(r).Ok() << ">" << std::endl;
};
yaclib::Run(tp, first).Then(second).Then(third).Then(fourth).Detach(last);
tp.SoftStop();
tp.Wait();
}
public:
}
return value + 1;
});
}
return value * 2;
});
}
private:
};
TEST(Example, AsyncPipeline) {
calculator.Increment(1)
.Then([&](int r) {
return calculator.Double(r);
})
return calculator.Increment(r);
})
std::cout << "Result: " << std::move(r).Ok() << std::endl;
});
tp.SoftStop();
tp.Wait();
}
std::move(p).Set(42);
std::cout << "Hello from the second thread pool!";
});
tp.SoftStop();
tp.Wait();
tp2.SoftStop();
tp2.Wait();
}
TEST(Example, StrandAsync) {
auto first = []() {
return 42;
};
auto second = [](int r) {
return r;
};
auto third = [](int r) {
return r + 1;
};
.Then(*strand, second)
.Then(tp, third)
std::cout << "Final result: " << std::move(r).Value() << std::endl;
});
tp.SoftStop();
tp.Wait();
}
yaclib::FutureOn< int > Increment(int value)
yaclib::FutureOn< int > Double(int value)
TODO(kononovk) Doxygen docs.
bool Valid() const &noexcept
Check if this Future has Promise.
Provides a mechanism to access the result of async operations.
auto Then(Func &&f) &&
Attach the continuation func to *this.
Encapsulated return value from caller.
auto Run(Func &&f)
Execute Callable func on Inline executor.
IExecutorPtr MakeStrand(IExecutorPtr e)
Strand is the asynchronous analogue of a mutex.
Contract< V, E > MakeContract()
Creates related future and promise.
TEST(Example, HelloWorld)