
Timercpp: JavaScript-like setTimeout and setInterval for C++ - delvincasper
https://github.com/shalithasuranga/timercpp
======
AHTERIX5000
Not trying to be mean but why submit something like this to HN? This snippet
just launches a thread per every timer using standard library and sleeps.
There is no way to really stop anything or knowing if timers are active etc.

Sure, it's syntactically valid C++ (probably) but that's about it.

~~~
ben0x539
I feel like there is a more constructive way to outline in which ways this
solution might need to be improved to be suitable for various real-world use-
cases, without focusing on whether this submission qualifies as HN content.

------
w-m
It has the same syntax as JavaScript, but unfortunately C++ is quite a
different language. As C++ has neither compiler check for lifetimes nor
garbage collection, this would be very easy to misuse in practice.

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.

~~~
BagOfPistchios
agreed, it is dangeorus if newbies try to use this

~~~
pjmlp
If it was only newbies doing such kind of errors. :\

------
csmattryder
It's missing `clearInterval(int)`, and these replicas return void instead of
an ID that I can use to nuke the execution.

I can't quite understand what this brings that I wouldn't find from a
std::thread constructor, besides less available documentation?

------
entelechy
This setTimeout implementation has very different semantics compared to
Javascript.

The main issue is it creates a thread per invocation instead of using an
eventloop.

~~~
udp
Exactly. And the eventloop that is available to a C++ application will differ
completely depending on framework/platform. The only thing common to all
platforms is that a thread is most likely to be the _wrong_ way to do this.

------
dsamarin
Seems like a very useful short snippet for creating threads without having to
learn a new threading API coming from a JS background.

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.

~~~
otabdeveloper1
> 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.

~~~
ChrisSD
Really? I thought Javascript was single threaded, aside from webworkers?

~~~
entelechy
you don't need parallelism in order to create deadlocks or race conditions

~~~
ChrisSD
I'm sorry but I'm not sure how a single threaded program can have race
conditions? Surely the very definition of a race condition excludes the
possibility? Or are our definitions different?

~~~
TeMPOraL
The event loop stops being deterministic if you start triggering events based
on a clock. Or if you have calls blocking on external resources with
unpredictable timing.

Say, you have a code like this:

    
    
      asynchronously doA();
      blockReadingFile();
      doB();
    

Whether doA() is finished or not when doB() executes depends on internals of
doA() and on how long blockReadingFile() takes.

------
Tihiy
Yep that's just as awful as JavaScript.

------
heavenlyblue
It seems author is making a point that modern C++ programming is degrading
towards the same illness JS packages are: we're now going to be using
libraries which are literally 60 lines of code in order to accomplish
something that could be done by the standard library.

------
robmccoll
You might want to do something atomic with that clear.

------
speps
Why is delvincasper posting projects from shalithasuranga's GitHub account?
Easy upvotes?

~~~
kierenj
Sharing good information isn't a crime?

~~~
speps
The problem is that it's not that good information but for some reason seems
to make it to the front page quite easily. Being critical of a project is a
good thing, this isn't it.

------
adgasf
This seems to spin up a thread for each `setTimeout`. The overhead of this is
massive.

