6

I'm looking at porting a game to HTML5, but I'm running into some problems.

The game uses multithreading quite a bit, with rendering running on the main thread and scripts running on worker threads. Multiple scripts can (and frequently do) run concurrently. It's got a fair amount of global data that any script can access, mostly to read from but occasionally to write to as well. Mechanisms are in place to ensure that thread safety is not a problem.

It looks like HTML 5 Web Workers can be used to set up multithreading, but getting the global data to work will be a big problem, as there doesn't appear to be any easy way to share data between workers. I could theoretically put all the global data in a shared worker and have each script worker send it messages to get and set data, but access is common enough that the message-passing overhead would completely destroy performance, especially since a simple read would be far more expensive than a write, and potentially require some sort of CPS implementation just to make it work.

Is there any good way to port an architecture like this to HTML5 and JavaScript?

Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
  • 3
    No 'better' way I'm afraid - http://stackoverflow.com/questions/2262681/sharing-variables-between-web-workers-global-variables. – Paul Grime Apr 04 '13 at 21:22
  • 2
    As Paul said, WebWorkers provide the only method of concurrency in JavaScript. You should try to trim down the shared data as most as possible. Note that some objects are actually [transferable](http://www.w3.org/html/wg/drafts/html/master/infrastructure.html#transferable-objects), which would prevent you from copying the results, but since you don't want to transfer the ownership of your state to your workers it's probably not useful for your general problem. – Zeta Apr 04 '13 at 21:25

1 Answers1

0

I wonder if you couldn't either use something like an in memory database like sqlite (or if more is needed, a full on postgres database or something), or alternately something like websockets, for implementing something like this. I haven't done much concurrency in js so I am not sure what the drawbacks would be. Of the two I sort of like the websockets idea better. Another option that occurs to me is to use some kind of a message queue, like rabbitmq or kafka.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 03 '22 at 15:18
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33308673) – Vojin Purić Dec 05 '22 at 15:59