views:

516

answers:

4

Is there any way for me to share a variable between two web workers? (Web workers are basically threads in Javascript)

In languages like c# you have:

public static string message = "";
static void Main()
{
 message = "asdf";
 new Thread(mythread).Run();
}
public static void mythread()
{
 Console.WriteLine(message); //outputs "asdf"
}

I know thats a bad example, but in my Javascript application, I have a thread doing heavy computations that can be spread across multiple threads [since I have a big chunk of data in the form of an array. All the elements of the array are independent of each other. In other words, my worker threads don't have to care about locking or anything like that]

I've found the only way to "share" a variable between two threads would be to create a Getter/setter [via prototyping] and then use postMessage/onmessage... although this seems really inefficient [especially with objects, which I have to use JSON for AFAIK]

LocalStorage/Database has been taken out of the HTML5 specification because it could result in deadlocks, so that isn't an option [sadly]...

The other possibility I have found was to use PHP to actually have a getVariable.php and setVariable.php pages, which use localstorage to store ints/strings... once again, Objects [which includes arrays/null] have to be converted to JSON... and then later, JSON.parse()'d.

As far as I know, Javascript worker threads are totally isolated from the main page thread [which is why Javascript worker threads can't access DOM elements

Although postMessage works, it is slow.

Thanks!

A: 

It's very strange(and interesting :) ) to do heavy computations in browsers javascript. I can't to imagine tasks, which really needed to this. May be cookies can help... but I'm not sure.

Adelf
Another example would be having a Ball on a worker thread. The ball has properties x, yThe main thread is rendering the ball via Canvas. If i have the Ball's x/y changed on the worker thread [which is the thread manipulating the ball] I have to postMessage("x y");I have to use getters/setters for the ball's x/y value, which is uglyEventually, there are more parameters, such as color, width, height.... and the above becomes a mess >_<
ItzWarty
You talking about Observer pattern :) http://en.wikipedia.org/wiki/Observer_pattern (wiki)http://www.codeproject.com/KB/scripting/Observer_Pattern_JS.aspx (usefull article)But your problem is to find any sharing mechanism... May be, SQLite from Google Gears?
Adelf
The problem with that approach is that SQL [from how i've worked with it before, note that i've only worked with MySQL] Only supports types like: text, int, basic types. You won't have objects. So I would still have to do stringify an object, and then JSON.parse it >_< The problem is that doing such a thing is slow.That is, unless, I actually create an SQL table for each object, and a link to another SQL table for each object in that object. It would get messy really quickly, though >_< [imagine I have multiple balls?]
ItzWarty
+1  A: 

No, but you can send messages to web workers which can be arrays, objects, numbers, strings, booleans, and ImageData or any combination of these. Web workers can send messages back too.

Eli Grey
+1  A: 

Web workers are deliberately shared-nothing -- everything in a worker is completely hidden from other workers and from pages in the browser. If there were any way to share non-"atomic" values between workers, the semantics of those values would be impossible to use with predictable results. Now, one could introduce locks as a way to use such values, to a certain extent -- you acquire the lock, examine and maybe modify the value, then release the lock -- but locks are very tricky to use, and since the usual failure mode is deadlock you would be able to "brick" the browser pretty easily. That's no good for developers or users (especially when you consider that the web environment is so amenable to experimentation by non-programmers who've never even heard of threads, locks, or message-passing), so the alternative is no state shared between workers or pages in the browser. You can pass messages (which one can think of as being serialized "over the wire" to the worker, which then creates its own copy of the original value based on the serialized information) without having to address any of these problems.

Really, message-passing is the right way to support parallelism without letting the concurrency problems get completely out of control. Orchestrate your message handoffs properly and you should have every bit as much power as if you could share state. You really don't want the alternative you think you want.

Jeff Walden
A: 

I recently read about (but have not used), shared workers. According to Share the work! Opera comes with SharedWorker support, support is only in the newest browsers (Opera 10.6, Chrome 5, Safari 5).

Kevin Hakanson