Search code examples
javascriptmultithreadinghtmlweb-worker

Multithreading in JavaScript for game development


I am thinking of developing a game in pure JavaScript and html5, without using any third party plugins. The problem I am facing is that I cannot find a way to separate different "modules" of the game into separate threads, like the render job, game logic, asset loading and so on.

Web Workers seem to be able to separate code into different threads, but the problem with them is the limited information I can pass between them. For example, for the render job, I need to pass the whole "world", with all the entities, meshes, textures and so on for every update of the game, because worker threads cannot share memory. It can be optimized, like sending the static objects only on initialization (meshes, textures) and then send only the state of an object on update (it's world transform) but it still isn't desirable.

Is there a way to send large data between them or have them share some objects? Or is there a different method entirely of achieving true multithreading? I know there are easier ways of achieving this using plugins/ gears but I need to use only methods available in open web;


Solution

  • JavaScript's Web Worker is a better concurrent programming model in some way. For example, it's event driven and there's no shared object. This means you can't get into grid lock (because there's no lock at all) and an object can't get into invalid state by being modified by two threads at the same time.

    The problem is you can't easily stack traditional game system on the top of this model. So you need to design the game system in a new way to adopt this programming model, and I think this might be costly.