












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to multithreading in c# through examples and explanations of threading, locks, pulse and wait, and polling. The use of threads, threading examples, lock examples, pulse and wait examples, and polling. It also includes advice on best practices for multithreading.
Typology: Exams
1 / 20
This page cannot be seen from the preview
Don't miss anything!













Lecture 13: Introduction to Multithreading Prof. Aaron Lanterman School of Electrical and Computer Engineering Georgia Institute of Technology
by Ben Albahari, Peter Drayton, and Brad Merrill, 2001 by^ Joseph^ Hall,^2008
4
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); } }
Example from “C# Essentials,” pp. 107-108.
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
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
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 ); } } }
Example from “C# Essentials,” p. 108
10
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( expression ) { //mycode } is syntactic sugar for System.Threading.Monitor.Enter(expression); try { // mycode } finally { System.Threading.Monitor.Exit(expression); } From “C# Essentials,” pp. 108-
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
#if XBOX Thread.CurrentThread.SetProcessorAffinity ( new int [] {index}); #endif
Advice from Joseph Hall, “XNA Game Studio Express,” p. 610