Sharing variables between web workers? [global variables?]

前端 未结 4 430
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-17 19:06

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:



        
相关标签:
4条回答
  • 2021-01-17 19:19

    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.

    0 讨论(0)
  • 2021-01-17 19:21

    There are two options to share data between dedicated workers:

    1. Shared Workers

    The SharedWorker interface represents a specific kind of worker that can be accessed from several browsing contexts, such as several windows, iframes or even workers.

    Spawning a Shared Worker in a Dedicated Worker

    2. Channel Messaging API

    The Channel Messaging API allows two separate scripts running in different browsing contexts attached to the same document (e.g., two IFrames, or the main document and an IFrame, two documents via a SharedWorker, or two workers) to communicate directly, passing messages between one another through two-way channels (or pipes) with a port at each end.

    How to call shared worker from the web worker?

    0 讨论(0)
  • 2021-01-17 19:21

    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).

    0 讨论(0)
  • 2021-01-17 19:34

    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 nearly 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.

    0 讨论(0)
提交回复
热议问题