Docsity

Introduction to Multithreading in C#: Threading, Locks, Pulse and Wait, and Polling - Prof, Exams of Electrical and Electronics Engineering

Partial preview of the text

Download Introduction to Multithreading in C#: Threading, Locks, Pulse and Wait, and Polling - Prof and more Exams Electrical and Electronics Engineering in PDF only on Docsity!

Lecture 13: Introduction to Multithreading Prof. Aaron Lanterman School of Electrical and Computer Engineering Georgia Institute of Technology

References

by Ben Albahari, Peter Drayton, and Brad Merrill, 2001 by^ Joseph^ Hall,^2008

4

Threading example output

using System using System.Threading; class ThreadTest { static void Main() { Thread t = new Thread( new ThreadStart(Go)); t.Start(); Go(); } static void Go() { for ( char c=‘a’; c <= ‘z’; c++) Console.Write(c); } }

abcdabcdefghijklmnopqrsefg

hjiklmnopqrstuvwxyztuvwxyz

Output:

Example from “C# Essentials,” pp. 107-108.

Lock example

using System; using System.Threading; class LockTest { static void Main() { LockTest lt = new LockTest(); Thread t = new Thread( new ThreadStart(lt.Go)); t.Start(); lt.Go(); } void Go() { lock ( this ) for ( char c=‘a’; c <= ‘z’; c++) Console.Write(c); } } this references the current instance of the class (can’t use this in static methods) lock takes a reference type; if another thread has already acquired a lock, this thread halts until the other thread lets it go Example from “C# Essentials,” p. 108

Pulse and wait

using System; using System.Threading; class MonitorTest { static void Main() { MonitorTest mt = new MonitorTest(); Thread t = new Thread( new ThreadStart(mt.Go)); t.Start(); mt.Go(); } void Go() { for ( char c=‘a’; c <= ‘z’; c++) lock ( this ) { Console.Write(c); Monitor.Pulse( this ); Monitor.Wait( this ); } } } release lock temporarily; go to sleep until another thread pulses me wake up next thread that is waiting on the object once I’ve released it Example from “C# Essentials,” p. 109

Pulse and wait example output

using System; using System.Threading; class MonitorTest { static void Main() { MonitorTest mt = new MonitorTest(); Thread t = new Thread( new ThreadStart(mt.Go)); t.Start(); mt.Go(); } void Go() { for ( char c=‘a’; c <= ‘z’; c++) lock ( this ) { Console.Write(c); Monitor.Pulse( this ); Monitor.Wait( this ); } } }

aabbccddeeffgghhiijjkkllmm

nnooppqqrrssttuuvvwwxxyyzz

Output:

Example from “C# Essentials,” p. 108

10

Breaking the deadlock

void Go() { for ( char c=‘a’; c <= ‘z’; c++) lock ( this ) { Console.Write(c); Monitor.Pulse( this ); if (c < ‘z’) Monitor.Wait( this ); } } Example from“C# Essentials,” p. 110

11

Lock: behind the curtain

lock( expression ) { //mycode } is syntactic sugar for System.Threading.Monitor.Enter(expression); try { // mycode } finally { System.Threading.Monitor.Exit(expression); } From “C# Essentials,” pp. 108-

Polling

  • Main thread checks flag variables set by the worker threads when they finish
  • Useful if main thread can do some stuff (e.g., eye-candy animation in a turn-based strategy game) independently of the worker threads (e.g. AI), but needs worker threads to finish before continuing (e.g. making the computer’s move)

Polling example

bool done = false; while (!done) { Thread.Sleep(0); done = true; for int(i = 0; i < ThreadDone.Length; i++) { done &= m_ThreadDone[i]; } } Code from Joseph Hall, “XNA Game Studio Express,” p. 608 Worker thread i sets m_ThreadDone[i]=true before it exits

Locating your threads on the Xbox 360

  • Set thread affinity within the worker

thread immediately after starting it

  • Don’t forget to call it, or your worker thread will be running on the same hardware thread as your main thread
  • Only available on Xbox 360 XNA Thread.CurrentThread.SetProcessorAffinity ( new int [] {index});

Check to see if you’re on an Xbox 360

#if XBOX Thread.CurrentThread.SetProcessorAffinity ( new int [] {index}); #endif

  • No way I know of in C# to manually set processor affinity in Windows like on the Xbox 360
  • Windows decides what threads run where

Advice

Advice from Joseph Hall, “XNA Game Studio Express,” p. 610

  • More than one thread per core isn’t bad…
  • …but more than one processor-intensive task per core is!
  • Put most intensive tasks on separate cores, and some less-demanding tasks on those same cores (threads that work in short bursts, disk I/O, etc.)

More advice

  • Limit number of synchronization points
  • Don’t lock resources longer than necessary
  • Avoid sharing data when possible
  • Profile your code before and after to make sure you’re getting the performance benefits you expect - Very easy to write multithreaded code that performs worse than single threaded! Advice from Joseph Hall, “XNA Game Studio Express,” p. 611