15.02.2015 Views

C# 4 and .NET 4

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

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

524 ❘ ChaPTer 20 threAds, tAsks, And synchrOnizAtiOn<br />

}<br />

}<br />

}<br />

Application.EnableVisualStyles();<br />

Application.SetCompatibleTextRenderingDefault(false);<br />

Application.Run(new Form1());<br />

semaphore<br />

A semaphore is very similar to a mutex, but, in contrast, the semaphore can be used by multiple threads<br />

at once. A semaphore is a counting mutex, meaning that with a semaphore you can define the number of<br />

threads that are allowed to access the resource guarded by the semaphore simultaneously. This can be used<br />

if you have several of the resources available <strong>and</strong> can allow only a specific number of threads access to the<br />

resource. For example, say that you want to access physical I/O ports on the system <strong>and</strong> there are three<br />

ports available. So, three threads can access the I/O ports simultaneously, but the fourth thread needs to<br />

wait until the resource is released by one of the other threads.<br />

.<strong>NET</strong> 4 gives you two classes with semaphore functionality: Semaphore <strong>and</strong> SemaphoreSlim. Semaphore<br />

can be named, use system-wide resources, <strong>and</strong> allow synchronization between different processes.<br />

SemaphoreSlim is lightweight version that is optimized for shorter wait times.<br />

In the sample application, in the Main() method six threads are created <strong>and</strong> one semaphore with a count<br />

of 4. In the constructor of the Semaphore class, you can define the count for the number of locks that can be<br />

acquired with the semaphore (the second parameter) <strong>and</strong> the number of locks that are free initially (the first<br />

parameter). If the first parameter has a lower value than the second parameter, the difference between the<br />

values defines the already allocated semaphore count. As with the mutex, you can also assign a name to<br />

the semaphore to share it among different processes. Here, no name is defined with the semaphore, so it is<br />

used only within this process. After the SemaphoreSlim object is created, six threads are started, <strong>and</strong> they<br />

all get the same semaphore.<br />

using System;<br />

using System.Threading;<br />

using System.Diagnostics;<br />

namespace Wrox.ProCSharp.Threading<br />

{<br />

class Program<br />

{<br />

static void Main()<br />

{<br />

int threadCount = 6;<br />

int semaphoreCount = 4;<br />

var semaphore = new SemaphoreSlim(semaphoreCount, semaphoreCount);<br />

var threads = new Thread[threadCount];<br />

for (int i = 0; i < threadCount; i++)<br />

{<br />

threads[i] = new Thread(ThreadMain);<br />

threads[i].Start(semaphore);<br />

}<br />

}<br />

for (int i = 0; i < threadCount; i++)<br />

{<br />

threads[i].Join();<br />

}<br />

Console.WriteLine("All threads finished");<br />

code snippet Semaphore/Program.cs<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!