.NET Threading Part II

ReaderWriterLock

Another popular design pattern introduced as a class in the .NET framework is the ReaderWriterLock. This class allows an unlimited amount of read locks or one write lock, but not both. This allows anyone to read the protected resource, as long as nobody is writing to the protected resource and allows only one thread to write to the protected resource at any one time. Listing 1 presents a sample using the ReaderWriterLock class.

Listing 1: ReaderWriterLock Class

using System;
using System.Threading;

namespace ConsoleApplication9
{
    class Class1
    {
        public Class1()
        {
            rwlock = new ReaderWriterLock();
            val = "Writer Sequence Number is 1";
        }

        private ReaderWriterLock rwlock;
        private string val;

        public void Reader()
        {
            rwlock.AcquireReaderLock(Timeout.Infinite);
            Console.WriteLine("Acquired Read Handle: "
                + "Value = {0}", val);
            Thread.Sleep(1);
            Console.WriteLine("Releasing Read Handle");
            rwlock.ReleaseReaderLock();
        }

        public void Writer()
        {
            rwlock.AcquireWriterLock(Timeout.Infinite);
            Console.WriteLine("Acquired Write Handle");
            int id = rwlock.WriterSeqNum;
            Console.WriteLine("Writer Sequence Number "
                "is {0}", id);
            Thread.Sleep(1);
            val = "Writer ";
            Thread.Sleep(1);
            val += "Sequence ";
            Thread.Sleep(1);
            val += "Number ";
            Thread.Sleep(1);
            val += "is ";
            Thread.Sleep(1);
            val += id;
            Console.WriteLine("Releasing Write Handle");
            rwlock.ReleaseWriterLock();
        }

        static void Main(string[] args)
        {
            Class1 obj = new Class1();

            const int n = 1000;
            Thread[] reader = new Thread[n];
            Thread[] writer = new Thread[10];

            for (int i= 0; i< n; i++)
            {
                reader[i] = new Thread(
                    new ThreadStart(obj.Reader));
                if (i< 10)
                {
                    writer[i] = new Thread(
                        new ThreadStart(obj.Writer));
                };
            }

            for (int i= 0; i< n; i++)
            {
                reader[i].Start();
                if (i< 10)
                {
                    writer[i].Start();
                };
            }
        }
    }
}

In the above listing, I create 10 writer threads and 1000 reader threads. I parameterized the number of reader threads so that I could quickly trigger different behaviors in the code by modifying the number of reader threads. Once the threads are started they attempt to acquire read and write lock on the ReaderWriterLock object. If you run the code, then you can see the writer threads have a difficult time acquiring write locks. I tried to put as many small sleep statements as I could to force the threads to swap out of memory earlier than they would have normally.

You might also like...

Comments

About the author

Randy Charles Morin

Randy Charles Morin Canada

Randy's article are Copyright 1998-2003 Randy Charles Morin

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.” - Dennis Ritchie