@loke
This is similar to the kind of functionality you'd have in plain C/C++/Fortran through the posix threads interface (that's likely not a coincidence).
And this is also why, in HPC, we strongly recommend people to always use a library to do things like this and never roll your own. There has to be several js libraries out there able to give you a higher-level abstraction for doing this.