Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;

like image 965
Colin Dumitru Avatar asked Jun 14 '11 19:06

Colin Dumitru


2 Answers

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.

like image 182
Cat Chen Avatar answered Sep 26 '22 00:09

Cat Chen


You probably want to look into Web Workers, I have no experience with them but have heard of them. Might lead you into the right direction.

http://ejohn.org/blog/web-workers/

like image 32
Jon Erickson Avatar answered Sep 23 '22 00:09

Jon Erickson