Home > DeveloperSection > Beginner > Thread Synchronization and Set Priority in C#

Thread Synchronization and Set Priority in C#


C# Thread  Synchronization 
Ratings:
0 Comment(s)
 2979  View(s)
Rate this:

Thread Synchronization and Set Priority in C#

In this article I’m explaining about Thread Synchronization in C#.

if you learn more  about Threading read my previous post Multithreading in c#

Synchronization:

In a multithreaded environment, each thread has its own local thread stack and registers. If multiple threads access the same resource for read and write, the value may not be the correct value. For example, let's say our application contains two threads, one thread for reading content from the file and another thread writing the content to the file. If the write thread tries to write and the read thread tries to read the same data, the data might become corrupted. In this situation, we want to lock the file access. The thread synchronization has two stages. Signaled and non-signaled.

The signaled state allows objects to access and modify data. The non-signaled state does allow accessing or modifying the data in the thread local stack.

Thread Synchronization on different Process:

Event:

Event is a thread synchronization object used to set the signaled or non-signaled state. The signaled state may be manual or automatic depending on the event declaration.

Mutex:

Mutex is the thread synchronization object which allows to access the resource only one thread at a time. Only when a process goes to the signaled state are the other resources allowed to access.

Semaphore:

Semaphore is a thread synchronization object that allows zero to any number of threads access simultaneously.

 

Example using Mutex:

 

using System.Threading;

 

namespace Synchronization

{

    class Program

    {

        private static Mutex mutex = new Mutex();

        private const int iterations = 1;

        private const int threads = 3;

 

        public static void Main()

        {

            for (int i = 0; i < threads; i++)

            {

                Thread thread = new Thread(new ThreadStart(ThreadProcess));

                thread.Name = String.Format("Thread{0}", i + 1);

                thread.Start();

            }

        }

 

        private static void ThreadProcess()

        {

            for (int i = 0; i < iterations; i++)

            {

                UseResource();

            }

        }

        private static void UseResource()

        {

            Console.WriteLine("{0} is requesting the mutex"Thread.CurrentThread.Name);

            if (mutex.WaitOne(1000))

            {

                Console.WriteLine("{0} has entered the protected area",Thread.CurrentThread.Name);

                Thread.Sleep(5000);

                Console.WriteLine("{0} is leaving the protected area",Thread.CurrentThread.Name);

                mutex.ReleaseMutex();

                Console.WriteLine("{0} has released the mutex",Thread.CurrentThread.Name);

            }

            else

            {

                Console.WriteLine("{0} will not acquire the mutex",Thread.CurrentThread.Name);

            }

        }

    }

}

 

Output

Thread Synchronization and Set Priority in C#

 

Thread Priority:

Thread priority specifies the scheduling priority of thread, and the executions of threads perform on its priority basis. When we create any thread and not assign its priority then by default normal priority will be assign for it. The thread priority does not affect the state of thread.

using System.Threading;

 

namespace ThreadPriority

{

    class Program

    {

        static void Main(string[] args)

        {

            Thread thread1 = new Thread(new ThreadStart(Thread1));

            Thread thread2 = new Thread(new ThreadStart(Thread2));

            thread1.Name = "Thread 1";

            thread2.Name = "Thread 2";

            thread2.Priority = System.Threading.ThreadPriority.Highest;

            thread1.Start();

            thread2.Start();

            Thread.CurrentThread.Name = "Main Thread";

            for (int i = 0; i < 20; i++)

            {

                Console.WriteLine(Thread.CurrentThread.Name);

            }

            Console.ReadKey();

        }

 

        public static void Thread1()

        {

            for (int i = 0; i < 10; i++)

            {

                Console.WriteLine("Thread 1:");

            }

        }

 

        public static void Thread2()

        {

            for (int i = 0; i < 10; i++)

            {

                Console.WriteLine("Thread 2:");

            }

        }

    }

}

 

Output

Thread Synchronization and Set Priority in C#

 i'll explain in my next post Creating, Managing and Destroying Threads in C#


Don't want to miss updates? Please click the below button!

Follow MindStick