.Net guid

Synchronization .Net

Yurii K
4 min readJun 17, 2019

How we can synchronize threads?

When we use threads or tasks they could use the same resource (property, field, file). One of the thread could change resource and sometime other threads will be worked with previous resource state that will create a lot of mistakes. To avoid that we must synchronize thread.

Lock

Lock is very simple to use. All what we need to do is create a variable for synchronization and create block of code, this code will unavailable for other threads until the current thread is finished.

Lock on line 26

On the left side we can see the result when we using a lock and on the right side we can see the results without using a lock.

With lock
Without lock

Monitor

Monitor working very similar to lock but it has some additional methods like “TryEnter(Object)”, “IsEntered(Object)”. Right now we are interested in methods “Enter(Object)” and “Exit(Object)”, those methods define start and end position of block of code which will be unavailable for other threads.

Monitor

Results will be the same like when we used “lock”.

Mutex

One of the coolest features of “Mutex” is a timeout. What if one of the thread will be work infinite? This thread take the resource and other threads are waiting when the resource will be free. When time expire the resource is free.

Mutex with a timeout on line 25

As we can see we do not need to create a specific variable for synchronization like in case with “Monitor” or “lock”.

Methods “WaitOne()” and “ReleaseMutex()” define a block of code which must be synchronized. “WaitOne()” method have some overloads. One of them take an int parameter, that is a timeout.

Important thing if we need to check if current thread still control the “mutex”, if answer is yes we must release it.

In this scenario “Mutex” will release himself every time because sleep value is always more than timeout.

Mutex with timeout

If we will remove parameter in “WaitOne()” method we will see a synchronization.

Mutex without timeout

Second features that we could used “mutex” across different programs, different process. That mean we could synchronize separated programs.

Mutex with name

For that we need to create “Mutex” using one of the constructor where second parameter is a name.

Mutex, Monitor and lock we are using when we resource must be used only by one thread.

Semaphore

“Semaphore” is similar to “Mutex” except one thing, “Semaphore” gives access to a resource by several threads simultaneously and we can control count of threads.

Semaphore

On line 8 we created a “Semaphore”. His constructor has two parameters: the first indicates how many objects the semaphore will initially be available, and the second parameter indicates what the maximum number of objects the given semaphore will use.

Just like a “Mutex” method “WaitOne()” has an override for timeout. Method “Release()” has an override, we control the count of vacant seats.

Result

We can see that two threads has an access to the resource because in constructor we passed “2, 2”. We can change those arguments as we like.

Originally published at http://tomorrowmeannever.wordpress.com on June 17, 2019.

--

--

Yurii K

Full-stack developer. In my work, I use Angular and C#. In my free time I write articles for my blog.