Harder Than It Seems? 5 Minute Timer in C++

preview_player
Показать описание


💰 Links to stuff I use:

This video is sponsored by Brilliant.
Рекомендации по теме
Комментарии
Автор

So… got any more comedy for me to look at? 👇

TheCherno
Автор

I am a Python programmer and this is how I would solve it:

import os
import sys
import time

# All done, Python takes 5 minutes to start

dhjerth
Автор

As a firmware engineer, my first instinct was "set the alarm peripheral to trigger an interrupt handler"

AJMansfield
Автор

Remember: this is the stuff we’re training our AI on

christopherweeks
Автор

Funny. I am an office application engineer and my first thought at looking at your code was We try to use as little resources as possible and a 5ms constant loop is "terrible" for battery life. It's funny how people from different coding worlds approach a problem different. My first instinct was much closer to the sleep/wait implementation (though I wouldn't waste an entire thread just to wait).

TwistedForHire
Автор

sleep doesn't take any cpu resources during the sleep time since the thread will be put into the pending queue (on most OS). Periodically checking will consume CPU time, even if it is minimal, and is a very Game Engine like solution.

add-iv
Автор

You want your users to hate you? Tell the user in the console to set a timer for 5 minutes, wait for them to press space and start the timer, and press space to finish it. :)

akashpatikkaljnanesh
Автор

I am not trying to protect topnik1's code in the video, it is pretty bad indeed BUT I am pretty sure it is not going to be 300 stack frames deep. From the looks of it, it is a tail recursive function, so any major compiler (e.g. clang) will do tail call optimization.

cubemaster
Автор

Trolling is a art; Topnik1 is a true artist.

systemhalodark
Автор

Good video overall, but the part about precision at 15:21 is a bit lacking. To be honest, precision is most likely not very important when sleeping for 5 minutes, but the overall takeaway of how sleep work is a bit wrong. Sleep will sleep for *AT LEAST* the time specified, but could sleep for quite a bit longer depending on other task's CPU utilization, the HPET (Hardware Precision Event Timer) used by the system (or not, on some system there might not even be one), the OS's timer resolution settings, the virtual timer resolution thing that windows do on laptops for powersaving where it will actually stretch the resolution, etc etc...
Therefore, when very high precision is desired (for example, a frame limiter in a game, to have smooth frame pacing), you don't want to sleep all the way, but rather, sleep for a significant portion of the time, but busy-loop at the end.
This fundamental misunderstanding of how sleeping work is why so many games have built-in frame limiters with absolutely garbage frame-pacing, and that you get a much smoother experience by disabling it and using something like RTSS's frame limiter instead.

Kazyek
Автор

"Train the AI using the entire internet, it will contain all of human knowledge."

The AI: "derp, but with extraordinary confidence"

asteriskman
Автор

In embedded land we have real timers! Talk about accurate... sub-nanosecond is easily doable. Overhead? It's a peripheral! Ignore it until it interrupts you.... or have it actually trigger an output without bothering you if you really need that accuracy. Love timers.

scowell
Автор

I recently made a discord bot. The task was giving every user a cooldown timer. Well at first glance it may seem like an insane task but once you realise time just goes on you don't have to do any computation meanwhile. You can just compare start and end whenever user needs to be updated. And this is how many village/base building games use on their playerbase. Like for example you need a buliding that takes 3 days to bulids. When the player is online you can just update every second but when the player is offline you can have the end date and compare to that when the player logs in again. Or someone interacts with that user.

brawldude
Автор

I'd never think i would ever spend 20 minutes to watch a 11 year old post about a 5 minute timer. but i learned something

Edit: 350 likes??? Damn i must be famous

xkleo
Автор

there is what I would argue as a better way than any of the methods described here that does not require async, it does not require threads, etc. It's just plain simple single threaded and it still allows your program to do other stuff while the timer runs.

what you do is you use the settimer function from C, and then register a signal handler for SIGALARM. in your signal handler, you unset the timer if you are done or you can keep it there if you want it to continue for the next 5 minutes.

hi
Автор

17:15 Btw, small nitpick for the C++14 users (and above): move your callback into the lambda capture, because if the callback is an object with a defined operator() (like a lambda), there could be big-ish members (like with a lambda capture).

kuhluhOG
Автор

25 years ago when I started coding I took setTimeout and setInterval in ActionScript for granted, I was 8.
Now I was thinking of a thread with a loop and events that trigger callbacks as other threads depending on timers you set that would mimick that behaviour but when you mentioned Promises I realized it would be way easier to open a thread for each timer and just sleep...

rogercruz
Автор

I'm surprised no one brought up interrupts, I don't know about modern C++, but I've seen in old school assembly this concept of setting a "flag" that interrupts execution and calls/executes a function before handing back the CPU.

andersonklein
Автор

A game-developer who cautions to not use OS-dependent libraries. Music in my Linux-gaming ears. 😉

peterjansen
Автор

The best solution for this is asynchronous execution. That way you can decide how the execution is performed i.e on the same thread or on a separate thread, and when the execution / timer is complete, you can decide if you want to rejoin the execution context (thread) back to main and take the perf hit, or run your logic on the background thread without any perf hit.
You get all the benefits i.e you don't need to worry about thread safety and its fully configurable in how you want it to run.

KieranDevvs
welcome to shbcf.ru