Home > Development > ReadWriterLockSlim and Tasks

ReadWriterLockSlim and Tasks


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace TPLSamples
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Program p = new Program();
            p.TestingReadWriterLocks();

            Console.WriteLine("Press any key to quit...");
            Console.ReadKey();
        }

        public void TestingReadWriterLocks()
        {

            CancellationTokenSource s = new CancellationTokenSource();
            var token = s.Token;

            ReaderWriterLockSlim rw = new ReaderWriterLockSlim();
            int datavalue = 0;


            try
            {
                Task[] readertasks = new Task[3];

                for (int i = 0; i <= readertasks.Length - 1; i++)
                {
                    readertasks[i] = new Task(() =>
                    {
                        while (true)
                        {
                            if (token.IsCancellationRequested)
                            {

                                Console.WriteLine("Reader Task: {0} Errored", Task.CurrentId);
                                throw new OperationCanceledException();
                            }
                            token.WaitHandle.WaitOne(200);
                            rw.EnterReadLock();
                            Console.WriteLine("Task: {0} Just Read: {1}", Task.CurrentId, datavalue);
                            rw.ExitReadLock();
                        }
                    }, token);
                    readertasks[i].Start();
                }

                Task writertask = new Task(() =>
                {

                    while (true)
                    {
                        if (token.IsCancellationRequested)
                        {

                            Console.WriteLine("Writer Task: {0} Errored", Task.CurrentId);
                            throw new OperationCanceledException();
                        }

                        token.WaitHandle.WaitOne(2000);
                        rw.EnterWriteLock();
                        ++datavalue;
                        Console.WriteLine("Task: {0} Just Wrote: {1}", Task.CurrentId, datavalue);
                        rw.ExitWriteLock();
                    }

                }, token);

                writertask.Start();

                Thread.Sleep(5000);
                s.Cancel();

                writertask.Wait(); // Cause exception to be thrown;
                Task.WaitAll(readertasks); // Cause exception to be thrown;

            }
            catch (AggregateException exception)
            {
                exception.Handle(ex =>
                {
                    if (ex.GetType() == typeof(OperationCanceledException))
                    {
                        OperationCanceledException ce = ex as OperationCanceledException;
                        Console.WriteLine(ce);
                        return true;
                    }

                    return false;
                });
            }
        }
    }
}

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: