6

Are there any synchronization primitives like Barriers, Semaphors, Locks, Monitors, ... available in JavaScript / Web Workers or is there some library available empowering me to make use of such things (I'm thinking of something like java.util.concurrent in Java)?

Do Workers have obscure properties which differentiate them from Threads (can they share memory with the main thread, for example)? Is there some kind of limit how many workers can be spawned (like, for security reasons or something...)? Do I have to take special care of something?

4 Answers 4

3

Web workers don't have a concept of shared memory; all messages that are passed between threads are copied. With that being said, you don't have Barriers, Semaphores, Locks, and Monitors, because you don't need them in the web worker model.

The concept of shared memory was proposed back in Feb 2011 but the status is now wontfix due to developer complexity => https://lists.webkit.org/pipermail/webkit-unassigned/2011-February/287595.html

There is also a nice blurb about web workers here. http://blogs.msdn.com/b/ie/archive/2011/07/01/web-workers-in-ie10-background-javascript-makes-web-apps-faster.aspx

Hope this helps

Sign up to request clarification or add additional context in comments.

2 Comments

So I would need to send a message back to the main thread who simply counts if all workers are done in order to implement something like a Barrier?
That would be an affirmative, messages are copied to and from the ui thread to the background thread. If your looking for a barrier, and other concepts of concurrency I think your better off going with going with a language that offers shared memory as a feature.
2

In short: no there aren't any synchronization primitives in javascript but there is also no need for them since JavaScript is inherently single threaded :). Workers can only access there own scope (no dom manipulation just calculations) and send messages to the main ui thread where the normal js resides. I'm not sure about the maximum count of workers but there sure is a limit, you could try it out in a browser :)

Hope this helps!

Comments

2

Here you have a library based on jQuery made for that purpose: http://www.megiddo.ch/jcon-q-rency.

Of course the model is not really identical to java.util.concurrent since we are not dealing with the same environment, as explained in the other answers...

Comments

1

Newer answer: web workers can share memory now via SharedArrayBuffer, although your site needs to be configured with cross-origin isolation in order to use this. But once you're using SharedArrayBuffer, it is possible to have one worker's execution synchronously block waiting on another worker thread, using Atomics.wait() and Atomics.notify() to get mutex behaviors.

Browsers don't allow calling wait() from the main thread, however. The main thread still can only interact with web worker threads asynchronously.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.