C# Source Code: Performing synchronised updates in a multithreaded environment
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
C# Source Code
Performing synchronised updates in a multithreaded environment
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Thursday, April 08, 2004
Hits:
598
Category:
Threading/Asynchronous operations
Article:
Using multiple threads allows you to partition your work into multiple asynchronous pieces. However, the ability to have multiple threads executing in a single process also adds risk and complexity. One of the main sources of errors when writting multithreaded applications is controlling the access to shared data. If this is not correctly sychronised then the data will often become corrupted and you will find these failures extremely difficult to replicate (especially if you are testing the code on a single process box). The code below demonstrates how to synchronise updates across multiple threads: using System; using System.Threading; //Copyright Andrew Baker (www.vbusers.com) namespace vbUsers.Threading { //This class demonstrates how to safetly run multiple update threads against a single object class DemoTheadSynchronisation { [STAThread] static void Main(string[] args) { //Create worker class, which will be simultaneously updated by four threads Worker oWorker = new Worker(); oWorker.JobsToDo = 1000; //Set this to false to allow unsychronised updating (which wail cause unpredictable results, //especially on multiple processor machines. oWorker.SynchThreads = true; //Forces the updates to be serialised //oWorker.SynchThreads = false; //Causes the code the updates not to be serialised and hence behave unpredictably //Define Thread 1 Thread th1 = new Thread(new ThreadStart(oWorker.DoWork)); //Define Thread 2 Thread th2 = new Thread(new ThreadStart(oWorker.DoWork)); //Define Thread 3 Thread th3 = new Thread(new ThreadStart(oWorker.DoWork)); //Define Thread 4 Thread th4 = new Thread(new ThreadStart(oWorker.DoWork)); //Start the threads running th1.Start(); th2.Start(); th3.Start(); th4.Start(); //Wait for the threads to end th1.Join(); th2.Join(); th3.Join(); th4.Join(); //Display the output Console.WriteLine("No. of task done: " + oWorker.JobsDone); Console.Read(); } } //Worker class (to be updated by multiple threads) public class Worker { protected int ziJobsDone = 0, ziJobsToDo = 0; protected bool zbSynchThreads = false; //Synchronisation object protected object zoSychObj = new Object(); //Determine if the updates are to be sychronised (see DoWork) public bool SynchThreads { get {return zbSynchThreads;} set {zbSynchThreads = value;} } //Set the number of jobs carried out public int JobsDone { get{return ziJobsDone;} } //The number of jobs to perform public int JobsToDo { get{return ziJobsToDo;} set{ziJobsToDo = value;} } //Carry out an update loop public void DoWork() { /* Important note, C# guarantees some operations are uniterruptible (know as atomic operations). This includes reads an writes of 32-bit (or smaller) scalar values and the assignment of references. More complex operations, such as 64-bit scalar and floating point operations can be interputed. The ++ and -- operators are NOT atomic as they require a value to be read, updated and then written. Note, to do atomic updates you could also use: Interlocked.Increment(ref iMyCounter); */ if(zbSynchThreads==true) //Sychronised updating for(int i=0; i
Terms and Conditions
Support this site
Download a trial version of the best FTP application on the internet