DOC PREVIEW
GT ECE 4893 - Introduction to Multithreading
School name Georgia Tech
Pages 75

This preview shows page 1-2-3-4-5-35-36-37-38-39-71-72-73-74-75 out of 75 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 75 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 75 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 75 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 75 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 75 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 75 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 75 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 75 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 75 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 75 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 75 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 75 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 75 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 75 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 75 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 75 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Introduction to Multithreading Prof. Aaron Lanterman School of Electrical and Computer Engineering Georgia Institute of Technology2 References (1) by Ben Albahari, Peter Drayton, and Brad Merrill, 2001 by Joseph Hall, 20083 References (2) Francisco Balena 2006 Microsoft Press4 References (3) Tons of stuff from Microsoft’s Bruce Dawson5 Threading example 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);! }!}!static methods are part of the class, not particular instances Example from “C# Essentials,” pp. 107-108.6 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.7 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. 1088 Locks example output 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);! }!}!abcdefghijklmnopqrstuvwxyz!abcdefghijklmnopqrstuvwxyz!Output: Example from “C# Essentials,” p. 1089 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-109Lock advice from MSDN 10 • “In general, avoid locking on a public type, or instances beyond your code's control… – lock(this) is a problem if the instance can be accessed publicly. – lock(typeof(MyType)) is a problem if MyType is publicly accessible. – lock(“myLock”) is a problem since any other code in the process using the same string, will share the same lock.” http://msdn.microsoft.com/en-us/library/c5kehkcz(VS.80).aspxLock advice from Rico Mariani 11 class MyClass { " private static String myLock = “MyLock“;### ! public void Foo()#{"#### lock(myLock) { ... } "## } "}!• “This is bad because string literals are normally interned, meaning that there is one instance of any given string literal for the entire program. The exact same object represents the literal…on all threads. So if someone else comes along and locks a literal named “MyLock” his literal will interfere with yours. • Recommendation: private static Object myLock = new Object();!http://blogs.msdn.com/ricom/archive/2003/12/06/41779.aspxDon’t lock on value types 12 • Value types can be “boxed” to act as reference types… • …but each lock construct will create a different boxGrrrrrrrrrrr!!!!! • XNA on Xbox 360 uses Compact Framework, not full .NET like on Windows • Compact Framework has a Monitor class (so can use locks), but it doesn’t implement Pulse/Wait and their variations  • Also missing Semaphores • Not sure about “pro Xbox 360 development,” i.e. C++ XDK 1314 One Mutex From F. Balena, “Visual C# 2005: The Base Class Library,” p. 478. // This Mutex object must be accessible to all threads.!Mutex m = new Mutex();!public void WaitOneExample();!{! // Attempt to enter the synchronized section,! // but give up after 0.1 seconds! if (m.WaitOne(100, false))! {! // Enter the synchronized section.! …! // Exit the synchronized section, and release the Mutex.! m.ReleaseMutex();! }!}! A mutex is called “signalled” if no thread currently owns it15 Many Mutexes - WaitAny static Mutex[] mutexes = ! { new Mutex(), new Mutex(), new Mutex() };!public void WaitAnyExample();!{! // Wait until a resource becomes available.! // (Returns the index of the available resource.)! int mutexNdx = Mutex.WaitAny(mutexes);! // Enter the synchronized section.! // (This code should use only the! // resource corresponding to mutexNdx.)! …! // Exit the synchronized section, and release the Mutex.! mutexes[mutexNdx].ReleaseMutex();!}!From F. Balena, “Visual C# 2005: The Base Class Library,” p. 479.16 Many Mutexes - WaitAll • Wait until all resources have been released • Useful if you can’t proceed until all the other threads are done Mutex.WaitAll(mutexes)!From F. Balena, “Visual C# 2005: The Base Class Library,” pp. 480.17 Naming a Mutex (available on Windows) • If a Mutex with that name already exists, caller gets a reference to it; otherwise a new Mutex is created • Lets you share Mutex objects among different applications – Not too relevant to video game programming Mutex m = new Mutex(false,”mutexname”);!From F. Balena, “Visual C# 2005: The Base Class Library,” pp. 480.18 Mutexes vs. Monitor locks • Mutexes slower than locks (around 20 times slower!) – Monitor locks operating at the level of the CLR – Mutexes operate at the OS level • Mutexes generally reserved for interprocess communications (vs. interthread) Info from B. Dawson, “Coding For Multiple Cores on Xbox 360 and Microsoft Windows,” http://msdn2.microsoft.com/en-us/library/bb204834.aspx19 Thread safety • Some .NET objects are thread-safe • Some aren’t • Some .NET objects have some method that are thread safe and some that aren’t • Check the documentation • If using on Xbox 360, be careful to note .NET & Compact .NET differences Info from F. Balena, “Visual C# 2005: The Base Class Library,” pp. 473-474.20 Synchronized types • Some .NET types that aren’t ordinarily thread-safe offer thread-safe version // Create an ArrayList object, and add some values to


View Full Document

GT ECE 4893 - Introduction to Multithreading

Documents in this Course
Load more
Download Introduction to Multithreading
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to Multithreading and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to Multithreading 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?