-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How does one compose Streams with EventLoop Timers? #540
Comments
You're exactly right! The A few more things you'll need to do:
Also, we just landed a PR that should detect when you shouldn't be able to compose things. If you don't mind giving |
Following up here, I had forgotten that we had added support for running the event loop from the main thread when you're using the So if you write a int main() {
EventLoop::ConstructDefault();
return *SomeEventualFunctionThatReturnsInt();
} Then you should be all set. However, if you do something like this: int main() {
EventLoop::ConstructDefault();
auto task = Task::Of<int>([]() {
return SomeEventualFunctionThatReturnsInt();
});
std::future<int> result = task.Start("my task name");
result.wait(); // Your main thread will BLOCK here!
return result.get();
} Then your main thread will be waiting on the future and there won't be a thread to run the event loop. There is a variant here however that can help: int main() {
EventLoop::ConstructDefault();
auto task = Task::Of<int>([]() {
return SomeEventualFunctionThatReturnsInt();
});
std::future<int> result = task.Start("my task name");
EventLoop::Default().RunUntil(result); // Thanks for donating your main thread to run the event loop!
return result.get();
} It all depends on what you need! |
Hi Ben, Thanks for the replies, just getting back to this now. I was trying out what you said in your first message but couldn't get it to compile till just now. I missed that the argument to the lambda in a Map() needs to be an rvalue reference (&&), though that makes sense I think in retrospect. I also don't know why it didn't occur to me that Map could return an eventual instead of a synchronously created value. Now that totally makes sense. Similar to the PingPong example you pointed me to, the Loop() in my code is created by a second actor, and my initial tiny pipeline looks like:
I was thinking a cool toy example would be to simulate a few balls (circles) bouncing around in 2d space. Generating clock ticks can be a more platform dependent thing so I split it out. I might look into hooking up a third actor to do simple rendering or something. There are other possibilities like figuring out how to take some kind of keyboard/mouse input to allow interaction with the system (eg. fling a ball). Basically I just want to try doing something reasonably complicated. If it doesn't exist already, something I might try as a challenge is to see if I can create some kind of mechanism for sending ticks from the same clock to two different downstream actors, eg:
I did figure out the event loop setup and RunUntil in my main, and I found WaitForSignal so I'm using that to catch SIGINIT and stop my event loop which is super nice. |
@mthiffau ooh, I love a fun example like that. I once had one that drew a ball moving across a screen with We don't currently have a actorA.Stream()
>> Tee(actorB.Listen(), actorC.Listen())
>> Then(Unpack([](auto&& actor_b_listen_result, auto&& actor_c_listen_result) {
// ...
})); Or: actorA.Stream()
>> Tee(actorB.Listen(), actorC.Listen())
>> Map([](auto&& something_from_either_actor_b_or_actor_a) {
// ...
}); |
I'd like to create a Stream which produces "ticks" at even time intervals. My current solution can do this by blocking the the thread which is producing the ticks with sleep calls, but obviously it would be more optimal if I could make use of a Timer.
The following doesn't work (I think because the bare 'Then' doesn't handle the Stream stuff?). Is there something similar I could do? Or would an entirely different approach be better?
The text was updated successfully, but these errors were encountered: