C# Source Code: Creating a Thread-Safe Collection
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
C# Source Code
Creating a Thread-Safe Collection
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Friday, April 09, 2004
Hits:
726
Category:
Threading/Asynchronous operations
Article:
One of the most common places where thread safety is required is in a collection. When multiple threads contend for access to a collection you must provide synchronisation to prevent the collection from becoming corrupted. By default .NET collections aren't synchronised because this will reduce their performance and isn't neccessary in a single-threaded environment. Below are two methods of creating a synchronised (thread-safe) stack. using System; using System.Threading; using System.Collections; //Copyright Andrew Baker (www.vbusers.com) namespace vbUsers.Threading { //This class demonstates two methods for synchronising a stack class DemoStackThreadSafe { [STAThread] static void Main(string[] args) { //Create a thread safe stack with our custom class which //uses the SyncRoot property to provide synchronization StackThreadSafe customStack = new StackThreadSafe(50); //Populate stack customStack.Push("andrew"); customStack.Push("james"); customStack.Push("baker"); //Display contents Console.WriteLine("Contents from custom stack: "); foreach(string sName in customStack) { Console.WriteLine(sName); } //Create a thread safe stack with using the static Synchronized //method (note, ALL calls to a class created in this fashion go through a Sync //wrapper class which ensures synchronization). Stack SyncStack = Stack.Synchronized(new Stack(50)); //Populate stack SyncStack.Push("andrew"); SyncStack.Push("james"); SyncStack.Push("baker"); //Display contents Console.WriteLine("Contents from Synchronized stack: "); foreach(string sName in SyncStack) { Console.WriteLine(sName); } Console.ReadLine(); } } //Custom implementation of a thread safe stack //Enables you to choose which calls are sychronised //(in the case below all the calls have been sychronised). class StackThreadSafe: Stack { //:base will call Stack.Stack() public StackThreadSafe(): base() {} //:base(col) will call Stack.Stack(ICollection col) public StackThreadSafe(ICollection col): base(col) {} //:base(intialCapacity) will call Stack.Stack(int intialCapacity) public StackThreadSafe(int intialCapacity): base(intialCapacity) {} public override object Pop() { lock(SyncRoot) { return base.Pop(); } } public override object Peek() { lock(SyncRoot) { return base.Peek(); } } public override void Push(object obj) { lock(SyncRoot) { base.Push(obj); } } public override object[] ToArray() { lock(SyncRoot) { return base.ToArray(); } } public override int Count { get { lock(SyncRoot) { return base.Count; } } } public override void Clear() { lock(SyncRoot) { base.Clear(); } } public override bool Contains(object obj) { lock(SyncRoot) { return base.Contains(obj); } } } }
Terms and Conditions
Support this site
Download a trial version of the best FTP application on the internet