50
Multi-Threading and Load Balancing Compiled by Paul TaylorCSE3AGR Stolen mainly from Orion Granatir http://www.gamasutra.com/php-bin/article_display.php? story=4006

Multi-Threading and Load Balancing Compiled by Paul TaylorCSE3AGR Stolen mainly from Orion Granatir

Embed Size (px)

Citation preview

Multi-Threading and Load Balancing

Compiled by Paul TaylorCSE3AGRStolen mainly from Orion Granatir

http://www.gamasutra.com/php-bin/article_display.php?story=4006

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!

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

It all goes bad....• http://www.gamasutra.com/php-bin/article_display.php?story=4006

Multi-Core and Threading• http://www.gamasutra.com/php-bin/article_display.php?story=4006

Network Assimilation• http://www.gamasutra.com/php-bin/article_display.php?story=4006

Processes and Threads

The first crappy threading

code you’ll see

CATCTATCATTCTATCTACTATCATCTACTATCTACTATCTACTCT

TACTCAT

Distributing Load (You need to manage your employees)

That was a better Threading ModelBut still not good enough for

today's machines

Distributing Load even more!

Learning to ShareAll cats need the Shovel for their

Quest

Learning to ShareCat has the Shovel

Learning to ShareCat has the Loot

This is what Thread Locking is for

Now how do we go about locking them??

Critical Sections

• When one thread enters the critical section, the others will have to wait......

Critical Sections

• And wait....

• And wait...

Critical Sections

• Keep them small!!!• A lot of CPU time can be

wasted waiting.... And waiting...

Mutexs and Semaphores

Mutexs and Semaphores

Mutexs and Semaphores

Mutexs and Semaphores

Mutexs and Semaphores

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

Events

Events

Events

Events

Events

Events

Atomic Operations

• http://www.breadonthewaters.com/add/0888_nuclear_explosion_large_clipart.jpg

Atomic Operations

5 + 1 + 1 + 1 = 8

Atomic Operations

5 + 1 + 1 + 1 = 6!!

Atomic Operations

• This is why we need atomic operations

Atomic Operations

• This is why we need atomic operations

Deadlocks

Deadlocks

Deadlocks

Race Conditions

• The ‘race’ is a term for when your code expects threads to return in order, and they don’t!!!

Race Conditions

Race Conditions

Race Conditions

Network Code Example

Network Code Example

Network Code Example

Network Code Example

Network Code Example

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?

References

• http://www.gamasutra.com/php-bin/article_display.php?story=4006