I am here again to continue the discussion around Threading. Today, we will discuss synchronization context and the related concepts.
Links are given below to my previous parts:
Let’s start by putting forth some questions in order to understand the concepts.
What is Synchronization Context in Multithreading and when it is used?
Synchronization Context is yet another construct to control and manage thread execution in a multi threaded environment. It is used when we want a class level locking mechanism without manually locking the individual methods or properties.
How is Synchronization Context different from other constructs like lock, mutex and semaphore?
Synchronization Context provides automatic class or object level locking whereas other constructs like lock, mutex and semaphore are designed to implement manual locking for any specific code block.
How does Synchronization Context work?
If any class is annotated with Synchronization keyword and derived from ContextBoundObject, then .NET runtime puts an object level lock on all the methods, properties and fields of a class and hence only a single thread can access the members at one point of time.
We will understand this with the help of a simple example.
Let’s begin with defining the thread unsafe class or regular class as follows.
- public class RegularClass
- {
- List < string > nameList = newList < string > ();
- public void SetItem(string name)
- {
- nameList.Add(name);
- }
- public void PrintItems(objectthreadName)
- {
- Console.WriteLine("Thread {0} started executing PrintItems", threadName.ToString());
- Thread.Sleep(500);
- foreach(stringstrinnameList)
- Console.WriteLine(str);
- Console.WriteLine("Thread {0} finished executing PrintItems", threadName.ToString());
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Console.Title = "SynchronizationContext Demo";
- Threadthread1 = null;
- Threadthread2 = null;
- Threadthread3 = null;
- RegularClassrClass = newRegularClass();
- rClass.SetItem("Prakash");
- rClass.SetItem("Aradhana");
- thread1 = newThread(newParameterizedThreadStart(rClass.PrintItems));
- thread1.Start("thread1");
- thread2 = newThread(newParameterizedThreadStart(rClass.PrintItems));
- thread2.Start("thread2");
- thread3 = newThread(newParameterizedThreadStart(rClass.PrintItems));
- thread3.Start("thread3");
- }
- }
Output
As you can see in the output shown above even before thread1 finishes executing the PrintItems method, thread2 starts execution of the same method. Clearly, it’s not thread safe.
Now let’s have a look at the thread safe version using Synchronization Context as follows.
- [Synchronization]
- public class SynchronizationContexClass: ContextBoundObject
- {
- List < string > nameList = newList < string > ();
- public void SetItem(string name)
- {
- nameList.Add(name);
- }
- public void PrintItems(objectthreadName)
- {
-
-
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
-
-
- SynchronizationContexClassscClass = newSynchronizationContexClass();
- scClass.SetItem("Prakash");
- scClass.SetItem("Aradhana");
- thread1 = newThread(newParameterizedThreadStart(scClass.PrintItems));
- thread1.Start("thread1");
- thread2 = newThread(newParameterizedThreadStart(scClass.PrintItems));
- thread2.Start("thread2");
- thread3 = newThread(newParameterizedThreadStart(scClass.PrintItems));
- thread3.Start("thread3");
- }
- }
Output
As you can see in the output shown above, that when thread1 finishes executing the PrintItems method, then only the thread2 starts the execution and so on. Clearly it's thread safe.
Summary
In this article we discussed what Synchronization Context is and illustrated with an example how it works. Although it provides an automatic class level locking and is recommended to be used only when the other manual locking mechanism like lock/ mutex/ semaphore etc. doesn’t fit in a given scenario. Generally, manual locking is considered a better option as it’s less prone to a deadlock and provides better concurrency in comparison to a class level locking achieved by Synchronization Context.
You can also download the attached demo project (SynchronizationContextDemo.zip) to go through the source code referred to in the article.
Hope you have liked the article. Look forward for your comments/ suggestions.