Threads

From Valve Developer Community
Revision as of 23:13, 11 December 2008 by Sortie (talk | contribs) (First draft, source code to be included later)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Modern computers utilize multiple processors and recently games have begun to take advantage from this. By splitting code into multiple threads, it's possible to run code simutaniously on multiple processors. Threads are a very important part of Windows, where most programs use bunches of threads. Please note that the number of threads available is not limited by the number of CPUs, but by the memory in windows. Each thread will by default have a stack size of one megabyte, therefore Win 32 has a limitation of 2048 threads. This article is not about threads in general, but about threads in Source, and assumes you already know how threads work. For more information on how threads work in Windows please read about it on another site.

Source utilizes threads by putting each kind of operation into its own thread. Source has for instance a thread for physics, a thread for AI, a thread for rendering, and so on. If the computer does not have multiple CPUs, it will run every thread on the same CPU and switch between them every few miliseconds. Programmers using the Source SDK can use threads as well by using Valve's classes, and can run important functions, with a lot of waiting, asynchronously. These functions could for instance be WinSock operations, disk read/writes or similar. Functions that just need to wait for a while and then run some code would be better off being implemented by using the SetThink functions.