Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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. </p> <p>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.</p> <p>I would say take a look at <a href="http://www.nodejs.org/" rel="nofollow">node.js</a> 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 <a href="http://jsconf.eu/2009/video_nodejs_by_ryan_dahl.html" rel="nofollow">video</a>.</p> <p>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 <a href="http://documentcloud.github.com/backbone/docs/backbone.html#section-9" rel="nofollow">backbone.js</a>.</p> <p>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. </p> <p>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.</p> <p>Take for example this function</p> <pre><code>function(i) { j = i + 1; console.log(j); return foo(j); } </code></pre> <p>and converts it into this</p> <pre><code>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; } </code></pre> <p>your going to need to extend Function with <code>.suspend</code> and <code>.resume</code></p> <pre><code>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); </code></pre> <p>Also your going to have to replace all references to <code>var a = b()</code> with</p> <pre><code>callFunctionAsync(b, context, args, function(return) { var a = return; ... }); </code></pre> <p>I'll leave implentation upto you. For now all functions return an object <code>r</code> and only when <code>r.r</code> is set to a value has it "returned". So just check around the event loop whether it has "returned" yet by checking if <code>r.r</code> is set and if it has trigger the function async callback.</p> <p>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.</p> <p>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. </p> <p>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.</p> <p>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.</p> <p>If you cause dead-locks good luck to you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload