Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The trouble with games and globals is that games (nowadays) are threaded at engine level. Game developers using an engine use the engine's abstractions rather than directly programming concurrency (IIRC). In many of the highlevel languages such as C++, threads sharing state is complex. When many concurrent processes share a common resource they have to make sure they don't tread on eachother's toes.</p> <p>To get around this, you use <em>concurrency control</em> such as mutex and various locks. This in effect makes asynchronous critical sections of code access shared state in a synchronous manner for writing. The topic of concurrency control is too much to explain fully here.</p> <p>Suffice to say, if threads run with global variables, it makes debugging very hard, as concurrency bugs are a nightmare (think, <em>"which thread wrote that? Who holds that lock?"</em>).</p> <p>There <strong>are exceptions</strong> in games programming API such as OpenGL and DX. If your shared data/globals are pointers to DX or OpenGL graphics contexts then typically this maps down to GPU operations which don't suffer so much from the same trouble.</p> <p>Just be careful. Keeping objects representing 'player' or 'zombie' or whatever, and sharing them between threads can be tricky. Spawn 'player' threads and 'zombie group' threads instead and have a robust concurrency abstraction between them based on message passing rather than accessing those object's state across the thread/critical section boundary.</p> <p>Saying all that, I do agree with the <strong>"Say no to globals"</strong> point made below.</p> <p>For more on the complexities of threads and shared state see:</p> <p>1 <a href="https://computing.llnl.gov/tutorials/pthreads/" rel="noreferrer">POSIX Threads API - I know it is POSIX, but provides a good idea that translates to other API</a><br> 2 <a href="http://en.wikipedia.org/wiki/Category:Concurrency_control" rel="noreferrer">Wikipedia's excellent range of articles on concurrency control mechanisms</a><br> 3 <a href="http://en.wikipedia.org/wiki/Dining_philosophers_problem" rel="noreferrer">The Dining Philosopher's problem (and many others)</a><br> 4 <a href="http://www.cs.mtu.edu/~shene/NSF-3/e-Book/" rel="noreferrer">ThreadMentor tutorials and articles on threading</a><br> 5 <a href="http://software.intel.com/en-us/articles/multithreaded-game-programming-and-hyper-threading-technology/" rel="noreferrer">Another Intel article, but more of a marketing thing.</a><br> 6 <a href="http://portal.acm.org/citation.cfm?id=1231894.1231896" rel="noreferrer">An ACM article on building multi-threaded game engines</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload