From the course: Threading in C#

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Semaphore

Semaphore - C# Tutorial

From the course: Threading in C#

Start my 1-month free trial

Semaphore

- [Instructor] Now we'll talk about SemaphoreSlim. This was introduced in .NET Framework 4.0. Originally, we used to have semaphores, and semaphores are great, except they are a little resource-intensive. So, for example, they take about a thousand nanoseconds, which is about one microsecond. That's the overhead, whereas SemaphoreSlim, as the name suggests, is a slimmer version of semaphores, and it takes only 200 nanoseconds to create one. Now, what does semaphore and SemaphoreSlim do? They ensure that not more than a specified number of concurrent threads can access a particular resource. So, we're going to create a static SemaphoreSlim. Resolve the reference using system not threading. I'm going to call it SemaphoreSlim equals new SemaphoreSlim. And I'll give it the capacity of three. And the capacity, in this case, is only allowed three threads to access a certain resource. Okay, the for-loop, for, and then hit tab. Once you do that, you see I get... intellisense, and I can just…

Contents