0

I would like to create a test, research environment that would allow a web worker thread access to everything the main javascript thread has access to without any restrictions

methodmain
  • 230
  • 2
  • 11
  • it already has access via postMessage and event handlers... – dandavis Jun 10 '13 at 19:54
  • Javascript hosted in a browser is not a true multithreaded environment. Some primitives like locking are not available. How would you guarantee thread safety? – Robert Harvey Jun 10 '13 at 19:55
  • Why would you want that, to enable race conditions? WebWorkers are designed to communicate by message passing. Of course you *can* create a fork of any JS engine that allows shared memory access for research reasons, but you will need to modify the native code for that. It's impossible with plain js (or a bug) – Bergi Jun 10 '13 at 19:56
  • Sorry. I should have worded the question differently. I was wondering if anyone knew of an experimental browser release, or javascript implementation that would allow web workers to experience race conditions, etc. I wanted to try to create a framework that didn't require message passing. – methodmain Jun 10 '13 at 20:01
  • Message passing is, by far, the best way to approach this. You get all of the benefits of multithreading without all of the complexity. – Robert Harvey Jun 10 '13 at 20:03

1 Answers1

1

No, simply no.

You can pass messages to and from webworkers but their content can't be a host object.

From the MDN :

There's no access to non-thread safe components or the DOM and you have to pass specific data in and out of a thread through serialized objects. So you have to work really hard to cause problems in your code.

and

Note: As usual, background threads – including workers – cannot manipulate the DOM. If actions taken by the background thread need to result in changes to the DOM, they should post messages back to their creators to do that work.

You can give a description of your DOM objects from the user thread to the webworkers and from the webworkers give orders on what to do but that's about the most you can do to change DOM objects with web workers.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758