DOC PREVIEW
GT ECE 4893 - Pulse/Wait and Semaphores

This preview shows page 1 out of 4 pages.

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

Unformatted text preview:

11/3/09 1 Pulse/Wait and Semaphores Prof. Aaron Lanterman School of Electrical and Computer Engineering Georgia Institute of Technology 2 References (1) by Ben Albahari, Peter Drayton, and Brad Merrill, 2001 by Joseph Hall, 2008 3 Reference (2) Francisco Balena 2006 Microsoft Press 4 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. 10911/3/09 2 5 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 6 What’s the problem? 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 7 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 8 Impatient Wait • If another thread doesn’t pulse me within millisecondsTimeout, reacquire the lock and wake myself up • Return true if reactivated by monitor being pulsed • Return false if wait timed out public static bool Wait(object obj,! int millisecondsTimeout);!11/3/09 3 Grrrrrrrrrrr!!!!! • 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  • Not sure about “pro Xbox 360 development,” i.e. C++ XDK 9 10 Semaphores • Semaphores are good for restricting the number of threads accessing a resource to some maximum number • As far as I can tell, in XNA, semaphores only available on Windows  – Seems to be missing in Compact Framework and hence XNA on Xbox 360 – Not sure about “pro Xbox 360 development,” i.e. C++ XDK 11 Semaphores Semaphore sem = new Semaphore(2,2);!Initial count Max count sem.WaitOne(); // count from 2 to 1!sem.Release(); // count from 1 to 2!sem.Release(); // tries to bring! !! ! // count from 2 to 3,!!!! // but throws a!!!! // SemiphoreFull!!!! // exception!From F. Balena, “Visual C# 2005: The Base Class Library,” p. 482. 12 Semaphores Semaphore sem = new Semaphore(2,2);!Initial count Max count sem.WaitOne(); // count from 2 to 1!sem.WaitOne(); // count from 1 to 0!sem.WaitOne(); // Blocks until!!!! // another thread!!!! // calls sem.Release();!From F. Balena, “Visual C# 2005: The Base Class Library,” p. 482.11/3/09 4 13 Typical use of a semaphore Semaphore sem = new Semaphore(2,2);!void SemaphoreExample()!{ ! // Wait until a resource becomes available.! sem.WaitOne();! // Enter the synchronized section! // Exit the synchronized section, and! // release the resource! sem.Release();!} From F. Balena, “Visual C# 2005: The Base Class Library,” p. 478. 14 Semaphores: Naming and timeouts • Semaphores can be named like mutexes (makes sense on Windows) • Like event waits (pulse/wait from previous lecture), semaphore and mutex waits can be given timeout parameter, and return a boolean indicating whether they acquired the resource “naturally” or timed out 15 Semaphore with max count 1 vs. Mutex • A mutex or Monitor lock is owned by a thread; only that thread can release it • Semaphores can be released by


View Full Document

GT ECE 4893 - Pulse/Wait and Semaphores

Documents in this Course
Load more
Download Pulse/Wait and Semaphores
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 Pulse/Wait and Semaphores 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 Pulse/Wait and Semaphores 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?