Gamasutra is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Gamasutra: The Art & Business of Making Gamesspacer
Sponsored Feature: OMG, Multi-Threading is Easier Than Networking
View All     RSS
August 19, 2019
arrowPress Releases
August 19, 2019
Games Press
View All     RSS







If you enjoy reading this site, you might also want to check out these UBM Tech sites:


 

Sponsored Feature: OMG, Multi-Threading is Easier Than Networking


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

Related Jobs

HB Studios
HB Studios — Lunenburg/Halifax, Nova Scotia, Canada
[08.19.19]

Experienced Software Engineer
iGotcha Studios
iGotcha Studios — Stockholm, Sweden
[08.18.19]

Technical Artist
Deep Silver Volition
Deep Silver Volition — Champaign, Illinois, United States
[08.16.19]

Senior Engine Programmer
Square Enix Co., Ltd.
Square Enix Co., Ltd. — Tokyo, Japan
[08.16.19]

Experienced Game Developer





Loading Comments

loader image