17 points · by delvincasper · 8y ago · 31 comments
Sure, it's syntactically valid C++ (probably) but that's about it.
The Timer requires anything captured by the lambda to still be alive by the time the function is called. This may be obvious in C++, but it's still dangerous.
Even trying to use this as a set-and-forget-timer without any captures to work around this gotcha (like the example just printing stuff) won't work as the Timer object itself also needs to stay alive until the function is called. Do it further down the stack and the Timer is long destroyed when its function is supposed to run.
I can't quite understand what this brings that I wouldn't find from a std::thread constructor, besides less available documentation?
The main issue is it creates a thread per invocation instead of using an eventloop.
It seems to me like it would be more useful however to implement these functions using a queue and run loop so that we don't have to worry about hard-to-trace race conditions, etc.
Seems to me that something like 50% of clientside Javascript coding is "worrying about hard-to-trace race conditions", so maybe that's a feature, not a bug.