repeat_effect_until with a once called initialisation #1434
-
Sorry, I really try to understand the API, but it is sometimes quite hard with the sparse examples (or do I look at the wrong sources?) I'm trying to achive a repeated process, but with an initial set of async operations which shouldn't be repeated. auto repeated_work = then([](auto&& context) {
// do some work
return value;
})
| then([](auto&& value){
// do smth with the value
return value;
})
| then([](auto&& value){ return value.valid() })
| exec::repeat_effect_until();
schedule(scheduler)
| then([](){ // do some long running work }
| then([](auto&&... values){ return context; }
// init finished.
// how to call repeated_work now?
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Try Take care when using #include <stdexec/execution.hpp>
#include <exec/single_thread_context.hpp>
#include <exec/repeat_effect_until.hpp>
#include <print>
class MyContext {
public:
explicit MyContext(int data): data_{data} {}
auto receiveMessage() {
return stdexec::just(data_);
}
private:
int data_;
};
int main()
{
exec::single_thread_context context;
auto scheduler = context.get_scheduler();
auto sndr = //
stdexec::when_all(
stdexec::then(stdexec::schedule(scheduler), [] { return MyContext{42}; }),
stdexec::just(1))
| stdexec::let_value([](MyContext& context, int& x) {
return //
context.receiveMessage()
| stdexec::then([&](int value) {
std::print("{}: {}\n", x, value);
return x++ == 3;
})
| exec::repeat_effect_until();
});
stdexec::sync_wait(sndr);
} |
Beta Was this translation helpful? Give feedback.
Try
let_value
. If your context type is move-constructible then this will be good enough. Otherwise, you need some helper senders.Take care when using
exec::repeat_effect_until
with the pipe operator. It can be surprising what will be repeated.