First of all, it's important to note that JavaScript is completely single-threaded. Multithreading emulation is not really the way to go. You are much better off relying on eventloops.
As mentioned, web workers can be used, but there is no match between browsers for them, so I will ignore web workers. Also, you cannot manipulate the DOM with web workers.
I would say node.js to reason about why event loops are a good alternative to multithreading. I think it pretty well touches on why this is a good alternative in this video .
Therefore, instead of having an array of functions and iterating over them, you can instead create an event and associate a set of functions with it and trigger the specified event. A very easy implementation of events can be found in backbone.js .
You cannot just pause a stream in JavaScript because there is only one. There is no way to pause or resume a function without a function in which there are dots.
There is only one way to imitate this. Write a JavaScript parser that will break your beautiful JavaScript construct and create a system that allows you to pause and resume JavaScript.
Take for example this function
function(i) { j = i + 1; console.log(j); return foo(j); }
and converts it to this
var bar = function(i) { var r = {}; var j = i + 1; var f = function() { console.log(j); var g = function() { return foo(j); }; onNext(g, arguments.callee, this, r); }; onNext(f, arguments.callee, this); return r; }
you will need to extend the function with .suspend and .resume
Function.prototype.suspend = function() { this.__suspended = true; } Function.prototype.resume = function() { this.__suspended = false; } function onNext(callback, function, context, returnObj) { if (!function.__suspended) { var cb = function() { Backbone.Events.unbind("run", cb); returnObj.r = callback.call(this); } Backbone.Events.bind("run", cb); } } setInterval(function() { Backbone.Events.trigger("run"); }, 5);
You will also have to replace all references to var a = b() with
callFunctionAsync(b, context, args, function(return) { var a = return; ... });
I will leave the implantation up to you. At the moment, all functions return an object r and only when rr set to a value does it "return". So just check around the event loop to see if it has been “returned”, checking if rr installed, and if it calls the async callback function.
Hey and see what we have. Emulated threads by running them around an event loop. It is much better to use the event loop initially in your code, and then simulate the threads through it.
Basically, your function runs the next line of its code when you go around the event loop again. And check if any “function” is paused or resumed when you go around the eventloop.
I did not implement the return of functions back to "functions" for short. It should not be too difficult to imitate.
Either use the event loop directly, or use fake threading methods and get a compiler to compile your code so that it doesn't look disgusting when coding it.
If you cause punches with dead locks.