Timercpp: JavaScript-like setTimeout and setInterval for C++
github.com
github.com
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.
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.1. Reentrancy. Flow of tasks can be interrupted by hardware interrupts and OS signals. In that case race conditions can happen by interrupt handlers. Bare metal embedded system programmers often have big headaches because of this.
2. JS implementations and JS API design can have race condition bugs. So JS API designers have to consider it. One of example is because of the order of queuing tasks/handlers. [1]
[1]: https://github.com/w3c/mediacapture-record/issues/150#issuec...
asynchronicity and parallelism (threads) are two entirely different concepts. Most GUI systems except BeOS's are asynchronous on a single thread. When you code in node you just have one big hidden while loop that does
while(true) {
doPostedEvents();
}
and executes your callbacks one after each other.They are. However, in the browser resources (such as scripts) will be loaded using multiple threads. Javascript will be executed on one thread at a time, but this execution will switch to another context at the appropriate breakpoints.
Javascript in the browser is a multithreaded cooperatively multitasking system.
With such a definition of multithreaded language wouldn't Brainfuck also be one?
However, that isn't the point. The point is that multiple Javascript scripts will be executing concurrently (via cooperative multitasking) and manipulating the same DOM elements.
This is absolutely a source of race conditions and a huge, HUGE pain in the ass for programmers who think Javascript is a 'single-threaded language'.
Are you sure about this ? I could not find anywhere something saying that the main JS execution would switch threads - and even if it did, it would not impact the observable behaviour of your JS code anyways. Everything I could find on google says "JS is single-threaded" (except for web workers of course).
Is this page wrong ? : https://itnext.io/how-javascript-works-in-browser-and-node-a... and do you have an example of problematic code ?