ReaderWriterLockSlim – synchronizing threads

ReaderWriterLockSlim is a synchronization mechanism. It allows many threads to read the same resource and the only one resource to write the resource.

Example below is not synchornized. There are four tasks and two of them can update myValue in the same time. It can cause that at the end myValue != 0.

        public void StartWithoutSynchronization()
        {
            int myValue = 0;

            //edits the value
            var taskA = new Task(() =>
            {
                for (int i = 0; i < 100000; i++)
                {
                    myValue++;
                }
            });

            //edits the value
            var taskB = new Task(() =>
            {
                for (int i = 0; i < 100000; i++)
                {
                    myValue--;
                }
            });

            //reads the value
            var taskC = new Task(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    Console.WriteLine("C " + myValue);
                }
            });

            //reads the value
            var taskD = new Task(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    Console.WriteLine("D " + myValue);
                }
            });

            taskA.Start();
            taskB.Start();
            taskC.Start();
            taskD.Start();
            Task.WaitAll(taskA, taskB, taskC, taskD);

            Console.WriteLine(myValue);

            Console.WriteLine("Finished");
        }

The example below is edited to be synchronized using ReaderWriterLockSlim. Now only one task at a time can write.

        public void StartSynchronized()
        {
            var rwLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
            int myValue = 0;

            //edits the value
            var taskA = new Task(() =>
            {
                for (int i = 0; i < 100000; i++)
                {
                    rwLock.EnterWriteLock();
                    myValue++;
                    rwLock.ExitWriteLock();
                }
            });

            //edits the value
            var taskB = new Task(() =>
            {
                for (int i = 0; i < 100000; i++)
                {
                    rwLock.EnterWriteLock();
                    myValue--;
                    rwLock.ExitWriteLock();
                }
            });

            //reads the value
            var taskC = new Task(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    rwLock.EnterReadLock();
                    Console.WriteLine("C " + myValue);
                    rwLock.ExitReadLock();
                }
            });

            //reads the value
            var taskD = new Task(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    rwLock.EnterReadLock();
                    Console.WriteLine("D " + myValue);
                    rwLock.ExitReadLock();
                }
            });

            taskA.Start();
            taskB.Start();
            taskC.Start();
            taskD.Start();
            Task.WaitAll(taskA, taskB, taskC, taskD);

            Console.WriteLine(myValue);

            Console.WriteLine("Finished");
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s