Simulating context switches in JavaScript? Simulating context switches in JavaScript? multithreading multithreading

Simulating context switches in JavaScript?


First it's important to mention that JavaScript is completely single threaded. Emulating multi-threading is really not the way to go. Your far better off relying on eventloops.

As mentioned web workers could be used but there's not really a cross-browser compliance for them so I'll ignore web workers. Also you can't do any DOM manipulation with Web workers.

I would say take a look at node.js to reasoning behind why event loops are a good alternative to multi-threading. I believe he touches quite nicely on why it's a good alternative in this video.

So rather then having an array of functions and iterating over them you can instead create an event and bind a set of functions to them and trigger said event. A very lightweight implementation of events can be found in backbone.js.

You can't just pause a thread in JavaScript because there is only one. There's no way to suspend or resume a function without a function have points in it.

There is only one way to emulate this. Write a JavaScript parser that rips your nicely build JavaScript apart and builds up a system that allows you to suspend and resume your JavaScript.

Take for example this function

function(i) {    j = i + 1;    console.log(j);    return foo(j);}

and converts it into 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;}

your going to need to extend 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);

Also your going to have to replace all references to var a = b() with

callFunctionAsync(b, context, args, function(return) {    var a = return;    ...});

I'll leave implentation upto you. For now all functions return an object r and only when r.r is set to a value has it "returned". So just check around the event loop whether it has "returned" yet by checking if r.r is set and if it has trigger the function async callback.

Hey and look what we've got. Were emulating threads by running them round the event loop. It's a lot better to use the event loop natively in your code rather then emulating threads through it.

Basically get your function to run the next line of its code when you go round the event loop again. And check whether the particular "function" is suspended or resumed when you go round your eventloop.

I didn't implement bubbling the return of the functions back up to the "function" for sake of brevity. This shouldn't be too hard to emulate.

Either use the event loop directly or use fake threaded methods and get a compiler to compile your code so that it doesn't look hideous when you code it.

If you cause dead-locks good luck to you.