03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Thread-Safe Objects ❘ 503<br />

Other Locking Classes<br />

The .NET Framework provides a few other primitive classes that you can use to<br />

provide locking mechanisms. Usually the lock statement and the Monitor class are<br />

easier to use, so you should use those if possible.<br />

The Mutex class lets a process acquire a lock much as the Monitor class does. One<br />

big difference between Mutex and Monitor is that a Mutex can wait for a named<br />

mutex that is defined systemwide. That means you can use Mutex to coordinate<br />

among threads running in different programs.<br />

The SpinLock structure is like a high-performance Monitor class. Obtaining and<br />

managing locks involves some overhead, so creating and using a Monitor object<br />

can slow a thread down. Instead of creating a Monitor object, when a thread uses<br />

a SpinLock to enter a critical region, it enters a loop and spins until it can acquire<br />

the lock. This can improve performance if the thread will acquire the lock quickly.<br />

If the thread must spin for a long time (more than a few dozen milliseconds), then<br />

its loop will use up more CPU cycles than a Monitor object would, so the SpinLock<br />

will degrade the performance of other threads.<br />

The ReaderWriterLockSlim class is useful if a writer object needs exclusive write<br />

access to a resource and any number of reader objects need nonexclusive read access<br />

to the resource.<br />

The Semaphore class is similar to the Mutex class. The difference is that the<br />

Mutex class allows only one thread to access a critical region at one time, whereas<br />

a Semaphore allows a fixed number of threads to access a critical region at one<br />

time. For example, you could use a Semaphore to allow up to four threads to<br />

access a resource at the same time. Both Mutex and Semaphore let you lock<br />

threads locally within the same program or systemwide.<br />

For more information on these and a few even more exotic synchronization classes,<br />

see “Overview of Synchronization Primitives” at msdn.microsoft.com/library/<br />

ms228964.aspx.<br />

Thread-Safe Objects<br />

An object is thread-safe if it is safe for it and other objects of that class or other classes to run on different<br />

threads at the same time. For example, in the race condition example described earlier, the objects<br />

running in threads A and B were not thread-safe because they tried to read and update the same variable<br />

in an unsafe manner.<br />

If a class accesses only its own instance variables and no other class accesses those variables, then it<br />

is thread-safe.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!