3 ms·
Interesting. I don't know much about OSes, but in userspace there's an analog for event loops. Almost all of them have a timer heap or timer wheel or something
by 01HNNWZ0MV43FF 2y ago
Interesting. I don't know much about OSes, but in userspace there's an analog for event loops.
Almost all of them have a timer heap or timer wheel or something (that's below the level of abstraction I work at) which can always answer the question, "When do I wake up next?". And then every event is reducible to either a timer timeout or an external event like the file driver giving you data from a file, getting a packet from the network, mouse movement, etc.
Games often don't need this because they tick at 60+ Hz to render everything anyway, so it's easier to just hang everything off that and check every timer on every frame, it's going to be a tiny waste of CPU compared to the rest of the game logic.
However in one case I was writing an event-based program for work, using C++, with limitations on what dependencies I could pull in, so I set a fixed tick (something like every 1 second) and when I got external events, I just ran the whole tick function instantly. This introduced some latency but it was in places that didn't matter, and it made the code really easy to follow.
- londons_explore 2y agoIt is sad that event based code is so hard to read. I wish some programming language would let the programmer write the code as-if polling, but then transform the program automatically to something event based at compiletime. Eg. While(true): if (button_is_pressed) do_x() If (something_else) do_y() The Linux select() API is nearly that, although it's rather cumbersome.
- eurleif 2y agoWhy is that easier to read than something like this? function onButtonIsPressed() { doX(); } function onSomethingElse() { doY(); }
- krisoft 2y agoWith the second I have to potentially worry about thread safety. What if while i’m busy doing X in response to a button press something else happens and I also need to do Y? With the first version of the code it is pretty obvious what will happen (i will finish doing X and then I will do Y). With the second code I have to read my platform’s documentation to know if it will buffer the second event while the first is handled, or drop it, or call the handler from a different thread.
- eurleif 2y agoThat's a pretty fundamental thing to understand about the platform you're using, isn't it? You learn it once, and then you never have to learn it again. Plus, it's not entirely clear in your example what happens if "something else" occurs, and then while `do_y()` is being executed, a button press occurs. Is the button press event buffered, or is it dropped? Also, given that you're proposing automatic transformation of programs (by the compiler?), and your `while` loop would become shorthand for some unspecified other program, there would potentially be a lot of hidden magic to be aware of. Your program could even be transformed into a program which would process the events in multiple threads.