Our Properties: Gamasutra GameCareerGuide IndieGames Indie Royale GDC IGF Game Developer Magazine GAO
My Message close
Contents
Sponsored Feature: OMG, Multi-Threading is Easier Than Networking
 
 
Printer-Friendly VersionPrinter-Friendly Version
 


Part of:



Latest News
spacer View All spacer
 
February 10, 2012
 
Road to the IGF: Lucky Frame's Pugs Luv Beats
 
Analyst questions validity of unusual January NPD results [4]
 
DICE 2012: Blizzard's Pearce on World Of Warcraft's launch hangover
spacer
Latest Jobs
spacer View All     Post a Job     RSS spacer
 
February 10, 2012
 
Beachhead / Activision
Senior Operations Engineer
 
Sony Computer Entertainment America LLC
Manager Quality Assurance
 
Beachhead / Activision
Senior Front End Web Engineer
 
Beachhead / Activision
Senior Back End Web Engineer
 
Beachhead / Activision
User Experience Lead
 
Sony Computer Entertainment America LLC
Senior Software Engineer
spacer
Latest Features
spacer View All spacer
 
February 10, 2012
 
arrow Principles of an Indie Game Bottom Feeder [19]
 
arrow Postmortem: CyberConnect 2's Solatorobo: Red the Hunter [1]
 
arrow Jerked Around by the Magic Circle - Clearing the Air Ten Years Later [39]
 
arrow Building the World of Reckoning [4]
 
arrow SPONSORED FEATURE: TwitchTV - How to Build Community Around Your Game in 2012 [13]
 
arrow Happy Action, Happy Developer: Tim Schafer on Reimagining Double Fine [9]
 
arrow Building an iOS Hit: Phase 1 [11]
 
arrow Postmortem: Appy Entertainment's SpellCraft School of Magic [5]
spacer
Latest Blogs
spacer View All     Post     RSS spacer
 
February 10, 2012
 
Audio Passes: Success Through Layering
 
What the current RPG can learn from Diablo 1
 
Double Fine's Kickstarter Windfall: Will Patronage Supplant Traditional Game Publishing? [5]
 
The Principles of Game Monetization
 
Did DoubleFine Just break the publishing model for good? [14]
spacer
About
spacer Editor-In-Chief/News Director:
Kris Graft
Features Director:
Christian Nutt
Senior Contributing Editor:
Brandon Sheffield
News Editors:
Frank Cifaldi, Tom Curtis, Mike Rose, Eric Caoili, Kris Graft
Editors-At-Large:
Leigh Alexander, Chris Morris
Advertising:
Jennifer Sulik
Recruitment:
Gina Gross
 
Feature Submissions
 
Comment Guidelines
Sponsor
Features
  Sponsored Feature: OMG, Multi-Threading is Easier Than Networking
by Orion Granatir [Programming, Visual Computing]
11 comments Share on Twitter Share on Facebook RSS
 
 
April 29, 2009 Article Start Page 1 of 6 Next
 

[In his new Intel sponsored feature, part of the Visual Computing section of Gamasutra, former Insomniac and current Intel staffer Orion Granatir introduces threading by comparing it to networking in games.]

Prior to working at Intel, I worked on PlayStation 3 games at Insomniac Games. Most of the work I did at Insomniac dealt with multiplayer network code. When I came to Intel, I was able to focus on threading, eventually realizing that threading and network programming are similar in several ways.

It can be a challenge to understand threading. Using network programming as a comparison, this article intends to give you an introduction to threading so that by the end you will understand the basics of threading in a Microsoft Windows environment.

Why Thread?

Why thread? That's a good question. Why put up with all the challenges that come with threading an application?

In the past, CPUs saw consistent performance gains because of the increases in frequency. In modern microprocessors, however, this frequency gain is marginal, and most performance benefits come from an increased number of cores. Having more cores means the CPU can do more things at the same time. To maximize performance and features, an application needs to fully utilize the CPU -- and that means threading!

Imagine you want to update the artificial intelligence (AI) for a group of monsters in your game. If you have only one core, all of those monsters will need to be processed in order. However, if you have multiple cores, you can process several monsters at the same time.

More cores, and therefore more threads, means you can have more monsters in your game. Huzzah!

Threads Are Like Clients

Threads are surprisingly similar to clients, but better. Clients are separate machines that all work and process independently. Like clients, cores all work separately and can process independent work. However, cores don't have to communicate over the slow and scary interwebs. Also, cores can quickly share data and will never lag out.

So what exactly is a thread?

  • A thread is a series of instructions executed to complete a task.
  • Each process can have multiple threads. Threads share the memory and resources within a process.
  • Just like clients, threads can work independently.

Here is a process with one thread:

A process can have multiple threads:

Okay, let's see some code. This example creates four threads: one main thread and three threads that run the PrintLetter function.

CreateThread spawns a new thread. You tell it which function to run and pass it any associated data. In this example the three threads run in a loop. The first thread prints 'C', the second prints 'A', and the third prints 'T'. The main thread will continue to run and do its own work. Since all threads are running at the same time your output will be a seemingly random combination of 'C', 'A', and 'T'.

 
Article Start Page 1 of 6 Next
 
Comments

James Munro
profile image
I found this article interesting and easy to read, the images make the concepts even easier to understand. This would particularly suit students (like myself).

How is the best way to determine that a section of code would benefit from threading? I would imagine that in some cases the cost of creating a thread and dealing with synchronisation can be more expensive than regular serial execution. What the best locations/components of a game engine to attempt threading?

Finally, I know that you have developed the Smoke framework which was designed to be parallel from the start, but have you considered implementing threading support in a pre-existing (and open-source) game engine as a proof-of-concept. I think people might benefit from knowing how to integrate good threading functionality into an existing system that was not designed with concurrency in mind. I know of an example that implements a taskpool/scheduler approach into the Quake 3 engine, known as cq3: https://svn.bountysource.com/cq3/trunk/quake3-1.32b/ (by Nick Gildea).

Cheers,

James Munro

Bob McIntyre
profile image
I enjoyed the informal writing style on this. I also think it did a solid job of explaining the basics of threading. Good article!

Jean-Charles Miron
profile image
Very good article! Great use of pictures. I just have one question, in your last example, you created 3 threads for the UpdateNetwork() function, but if all 3 are waiting, whats the use of creating 3 threads when only one will be able to run it at a time. Unless all 3 go at it when the event is launched, but that just seems to defeat the purpose of creating the threads in this case... I understand the concept of threads if you were to lets say break a task up in 3, but in this case it just seems like they call the same function and seems quite pointless. Unless I'm missing something.

Great article again!

Casey Bodley
profile image
Excellent article highlighting one of the areas most lacking in game development literature. Great read, thank you!

Jean-Charles: That would be an example of data decomposition, where each of the worker threads are reading from a thread-safe queue of network messages.

Orion Granatir
profile image
Hey James,

Thanks for the comments ^_^
Download the beta of Parallel Studio here: http://www.intel.com/go/parallel/
Try out Parallel Studio's Amplifier to help locate bottlenecks. These are probably the locations where you'll see the best results for your threading efforts. You can thread most areas of you code, the biggest challenge is determining how much effort it will take to restructure your data for threading. Simple loops that do a lot of calculations and have no data dependencies between iterations is a great place to start.

Good suggestion on threading an open source application... I'll kick the idea around the office.
- Orion

Orion Granatir
profile image
Hey Jean-Charles,

Good question. This is obviously a simplified example. The idea is that all thread will wake up and start pulling messages off the network queue. Once a thread has grabbed a message, it starts processing it. For example, one thread might get a message about damage to a player and start updating that players local data. Another thread might get a message about an explosion and start processing the local physics and animation. This way the threads can do the work in parallel and decrease the total time required to process network messages. I hope this makes sense.

Thanks,
Orion

Orion Granatir
profile image
Bob and Casey,

Thanks for the feedback! Please let me know if there are any areas you'd like me to write about in the future. I'm always interested in hearing things that developers need/want/desire.

Richard Lyle
profile image
I remember attending this at the Austin GDC... it was the biggest waste of my time ever. It was just a rehash of the documentation from the MSDN how how to use the Win32 CreateThread function...

I suppose for a beginner it might be useful ... as a lead programmer for a MMO, it didn't help me.

Richard Lyle
profile image
Just to add... if you want to give out some useful information, discuss lock contention.. in other words when too many threads are trying to lock a critical section you actually hurt performance... also patterns such as non-blocking queues for communicating data between threads without the need for a lock would again be useful.


Robert Casey
profile image
Great article covering the essentials of multithreading. Even an experienced developer can use a little rehash of what it's all about. The diagrams are a great teaching tool to get us past the jargon and abstract concepts.

Orion Granatir
profile image
Hey Richard,

Thanks for you feedback! Sorry about your impressions from GDC :-/ This article (and the associated GDC presentation) was intended for a beginner audience. I'm glad you told me the topics that interest you. I'll work on collateral that helps with the topics you suggested. Please let me know if you have any other thoughts.


none
 
Comment:
 




UBM Techweb
Game Network
Game Developers Conference | GDC Europe | GDC Online | GDC China | Gamasutra | Game Developer Magazine | Game Advertising Online
Game Career Guide | Independent Games Festival | Indie Royale | IndieGames

Other UBM TechWeb Networks
Business Technology | Business Technology Events | Telecommunications & Communications Providers

Privacy Policy | Terms of Service | Contact Us | Copyright © UBM TechWeb, All Rights Reserved.