Page 1
Multi-Threading and Load Balancing
Compiled by Paul TaylorCSE3AGRStolen mainly from Orion Granatir
http://www.gamasutra.com/php-bin/article_display.php?story=4006
Page 2
What changed?
• Single Thread Computer games have been used since day 0
• The person to blame is Gordon Moore*
* Not really it just feels good to blame someone!
Page 3
Moore’s Law
• http://en.wikipedia.org/wiki/File:Transistor_Count_and_Moore%27s_Law_-_2008.svg
•The Complexity of IC’s will go up exponentially•We mutilated this into CPU speed will increase expo.•This all F*#ked up a few years ago when we started pushing the boundaries of transistor miniaturisation•To keep the processing increase we went with Multi core processors
Page 4
It all goes bad....• http://www.gamasutra.com/php-bin/article_display.php?story=4006
Page 5
Multi-Core and Threading• http://www.gamasutra.com/php-bin/article_display.php?story=4006
Page 6
Network Assimilation• http://www.gamasutra.com/php-bin/article_display.php?story=4006
Page 7
Processes and Threads
Page 8
The first crappy threading
code you’ll see
CATCTATCATTCTATCTACTATCATCTACTATCTACTATCTACTCT
TACTCAT
Page 9
Distributing Load (You need to manage your employees)
Page 10
That was a better Threading ModelBut still not good enough for
today's machines
Page 11
Distributing Load even more!
Page 12
Learning to ShareAll cats need the Shovel for their
Quest
Page 13
Learning to ShareCat has the Shovel
Page 14
Learning to ShareCat has the Loot
Page 15
This is what Thread Locking is for
Now how do we go about locking them??
Page 16
Critical Sections
• When one thread enters the critical section, the others will have to wait......
Page 17
Critical Sections
• And wait....
Page 19
Critical Sections
• Keep them small!!!• A lot of CPU time can be
wasted waiting.... And waiting...
Page 20
Mutexs and Semaphores
Page 21
Mutexs and Semaphores
Page 22
Mutexs and Semaphores
Page 23
Mutexs and Semaphores
Page 24
Mutexs and Semaphores
Page 25
Mutexs and Semaphores
• A Semaphore is like usingMultiple tokens (it retains a value)• Critical Sections are fastest, but limited to one
process• Mutexs are slower, but globally accessible• Semaphores are great for things like allocating
your memory buffers
Page 32
Atomic Operations
• http://www.breadonthewaters.com/add/0888_nuclear_explosion_large_clipart.jpg
Page 33
Atomic Operations
5 + 1 + 1 + 1 = 8
Page 34
Atomic Operations
5 + 1 + 1 + 1 = 6!!
Page 35
Atomic Operations
• This is why we need atomic operations
Page 36
Atomic Operations
• This is why we need atomic operations
Page 40
Race Conditions
• The ‘race’ is a term for when your code expects threads to return in order, and they don’t!!!
Page 44
Network Code Example
Page 45
Network Code Example
Page 46
Network Code Example
Page 47
Network Code Example
Page 48
Network Code Example
Page 49
Network Code Example
• The Main thread would then need to wait for all Network threads to finish before it resets the NetworkUpdateEvent and continues
• How would you solve this?
Page 50
References
• http://www.gamasutra.com/php-bin/article_display.php?story=4006