????? C# ?? ????? ?? ?? multithreading ??????? ??? ???? ???? ?????? ?? ????? ?? ?????????? ???? ?? ??? ???? ????

???? ?????? ???? ??????
???? ID: 816161 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

?? ????? ?? ?????? ????? Microsoft Visual C# ??????????? ??? ?? ?? ??? ??? multithreading ?? ????? ????? Multithreading ??? ?? ?? ??? ??? ??????? ??????? ?? ???? ???? ?? ??? ????? ????? ??????? ?? ???? ???? Multithreading ??????????? ?? ???? ??????????? ?? ??????????? ?? ?? ??????? ???

?????? ????? ?? ?? ?? ??? ??? ?? ?????? ?? ????? ???? ???, ??????? ?? ???? ????????? ?? ???? ????? ?? ??? ???-??? ????? ?? ?????????? ???? ?? ??? ?? ????? ?? ???? multithreading ???????????? ?? ??? ??? ??????? ?????????? ?? ????? ???? ??, ?? ????? ?? ?? ???? ?????? ????? ?? ??? ???? ?????? ?? ????? ?? ?????????? ???? ?? ??? ???? ?????

??????? ??? ??? Multithreaded ??????? ??? ???? ??????? ???? ?? ???????? ???? ?? ??? ???

????????? ????????? ??? ??????? ???? ????????? ??? ??? ??????? ????? ????? ???? ????????? ????????? ?? ????? ?? ?????????? ???? ?? ??? ?? ????? ?? ???? ??? ?? ????? ????, ?? ????? ?? ???? ??ReaderWriterLock???????? ?? ????? ?? ????????? ???? ?? ??? ??? ??? ???? ?? ???, ????? ????? ?? ???? ????::
  1. Microsoft Visual Studio ??????? ?????
  2. ????? ???????????? ??,????? ????-????? ????, ?? ???? ????????????.
  3. ????? ????,????? C# ??????????? ???????????????? ???????? ????-????? ????, ?? ???? ???????? ??????????? ???????????????.

    ???:Visual Studio 2005 ?? Visual Studio 2008 ???, ????? ????????? C#?? ???????????????? ???????? ????-????? ????, ?? ???? ???????? ??????????? ???????????????.
  4. ??????????? ????? ???, ??????MultiThreadApplication?? ????-????? ????, ?? ???? ???OK.
  5. ?????? ??? Class1.cs ??? ????? ??? ?? ???????????? ?????

    ???:Visual Studio 2005 ??? ?? Visual Studio 2008 ???, ??????? ????? Program.cs ?? ???? ???
    using System;
    using System.Threading;
    
    namespace MultiThreadApplication
    {
    	class Class1
    	{
    		private ReaderWriterLock rwl = new ReaderWriterLock();
    		private long myNumber;
    		public long Number   // the Number property
    		{
    			get
    			{
    				//Acquire a read lock on the resource.
    				rwl.AcquireReaderLock(Timeout.Infinite);                
    				try
    				{
    					Console.WriteLine("Thread:{0} starts getting the Number", Thread.CurrentThread.GetHashCode());
    					Thread.Sleep(50);
    					Console.WriteLine("Thread:{0} got the Number", Thread.CurrentThread.GetHashCode());
    
    				}
    				finally
    				{
    					//Release the lock.
    					rwl.ReleaseReaderLock();
    				}
    				return myNumber;
    			}
    			set
    			{
    				//Acquire a write lock on the resource.
    				rwl.AcquireWriterLock(Timeout.Infinite);
    				try
    				{
    					Console.WriteLine("Thread: {0} start writing the Number", Thread.CurrentThread.GetHashCode());
    					Thread.Sleep(50);
    					myNumber = value;
    					Console.WriteLine("Thread: {0} written the Number", Thread.CurrentThread.GetHashCode());
    				}
    				finally
    				{
    					//Release the lock.
    					rwl.ReleaseWriterLock();
    				}
    			}
    		}
    		
    		[STAThread]
    		static void Main(string[] args)
    		{
    			Thread []threadArray = new Thread[20]; 
    			int threadNum;
    
    
    			Class1 Myclass = new Class1();
    			ThreadStart myThreadStart = new ThreadStart(Myclass.AccessGlobalResource);
    
    			//Create 20 threads.
    			for( threadNum = 0; threadNum < 20; threadNum++)
    			{
    				threadArray[threadNum] = new Thread(myThreadStart);
    			}
    
    			//Start the threads.
    			for( threadNum = 0; threadNum < 20; threadNum++)
    			{   
    				threadArray[threadNum].Start();
    			}
    
    			//Wait until all the thread spawn out finish.
    			for( threadNum = 0; threadNum < 20; threadNum++)
    				threadArray[threadNum].Join();
    
    			Console.WriteLine("All operations have completed. Press enter to exit");
    			Console.ReadLine();
    		}
    
    		public void AccessGlobalResource()
    		{
    			Random rnd = new Random();
    			long theNumber;
    			
    			if (rnd.Next() % 2 != 0)
    				theNumber = Number;
    			else
    			{
    				theNumber = rnd.Next();
    				Number = theNumber;
    			}
    
    		}
    	}
    }
    
  6. ????????? ?????, ?? ??? ????????? ??????

?????-???????? ???? ???? ????? ???? ?? ??? ???

?????? ????? ?? ?? ??? ??? ???? ???????? ?? ??????? ?? ?????? ?? ???? ???? ?? ?? ?? ???? ????? competes ???? ???????? ?? ????? ?? ??? ?? ?? ??? ???, ??? ??????? ??????? ?? ???? ?? ??? ?????? ?????? ??? ???? ???? ????? ?? ?? ?? ??? ??? ?????? ??????? ????? ?????? ?? ???, ??? ??? ????? ???? ???????? ?? ????? ?? ?? ???? ???? ????? ????? ?? ??????? ???? ??, ???? ????? ??????? ?? ???? ?? ????? ?? ?? ?????? ??????? ?? ?????? ?? ???? ??? ????????? ?? ??? ???

?? ?????? ?? ????, ?? ??? ?? locks ?? ????? ?? ???? ??? ?? ?????????? ???????? ??? ????????? ?? ???????? ???? ?? ??? ??? ?? ???? ???? ????? C# ?????? ????? ???, ?????? ???????? ???? ???? ??, ??? ????? ???? ???????? ?? ????? ???????? ?? ?????? ??????? ???? ?? ??? ???????? ?? ???? ??? ????? ?? ?????? ???? ??? ????? ?????? ??? demonstrate locks ??:
  1. Visual Studio ??????
  2. ????? ???????????? ??,????? ????-????? ????, ?? ???? ????????????.
  3. ????? ????,????? C# ??????????? ???????????????? ???????? ????-????? ????, ?? ???? ???????? ??????????? ???????????????.

    ???:Visual Studio 2005 ?? Visual Studio 2008 ???, ????? ????????? C#?? ???????????????? ???????? ????-????? ????, ?? ???? ???????? ??????????? ???????????????.
  4. ??????????? ????? ???, ??????MultiThreadLockApplication?? ????-????? ????, ?? ???? ???OK.
  5. ?????? ??? Class1.cs ??? ????? ??? ?? ???????????? ????:
    using System;
    using System.Threading;
    
    namespace MultiThreadLockApplication
    {
    	class Student
    	{
    		private static string myTeacherName = "Bill";
    		private string myName = "Grace";
    		private static object somePrivateStaticObject = new Object();
    
    		public static string TeacherName
    		{
    			get
    			{
    				string theName;
    
    				// Synchronize access to the shared member.
    				lock(somePrivateStaticObject)
    				{
    					Console.WriteLine("Thread {0} starts to get the teacher's name",Thread.CurrentThread.GetHashCode());
    					theName = myTeacherName;
    
    					// Wait for 0.3 second.
    					Thread.Sleep(300);
    					Console.WriteLine("Thread {0} finished to get the teacher's name:{1}.", Thread.CurrentThread.GetHashCode(), theName);
    				}
    				return theName;
    			}
    
    			set
    			{
    				lock(somePrivateStaticObject)
    				{
    					Console.WriteLine("Thread {0} starts to set the teacher's name.", Thread.CurrentThread.GetHashCode());
    					myTeacherName = value;
    
    					// Wait for 0.3 second.
    					Thread.Sleep(300);
    					Console.WriteLine("Thread {0} finished to set the teacher's name:{1}.", Thread.CurrentThread.GetHashCode(), value);
    				}
    			}
    		}
    
    		public string GetName()
    		{
    			string theName;
    			lock(this)
    			{
    				Console.WriteLine("Thread {0} starts to get the student's name.", Thread.CurrentThread.GetHashCode());
    				theName = myName;
    
    				// Wait for 0.3 second.
    				Thread.Sleep(300);
    				Console.WriteLine("Thread {0} finished to get the student's name:{1}", Thread.CurrentThread.GetHashCode(), theName);
    				return theName;
    			}
    		}
    
    		public string SetName(string NewName)
    		{
    			string theOldName;
    			lock(this)
    			{
    				Console.WriteLine("Thread {0} starts to set the student's name.", Thread.CurrentThread.GetHashCode());
    				theOldName = myName;
    				myName = NewName;
    
    				// Wait for 0.3 second.
    				Thread.Sleep(300);
    				Console.WriteLine("Thread {0} finished to set the student's name:{1}", Thread.CurrentThread.GetHashCode(), NewName);
    			}
    			return theOldName;
    		}
    	}
    
    	class Class1
    	{
    		public static int WorkItemNum = 20;
    		public static AutoResetEvent Done = new AutoResetEvent(false);
    
    		public static void AccessClassResource(object state)
    		{
    			Random rnd = new Random();
    			string theName;
    			Student AStudent = (Student) state;
    
    			if( (rnd.Next() %2) != 0)
    			{
    				if( (rnd.Next() %2) != 0)
    				{
    					switch (rnd.Next() %3 )
    					{
    						case 0:
    							Student.TeacherName = "Tom";
    							break;
    						case 1:
    							Student.TeacherName = "Mike";
    							break;
    						case 2:
    							Student.TeacherName = "John";
    							break;
    					}
    				}
    				else
    				{
    					theName = Student.TeacherName;
    				}
    			}
    			else
    			{
    				if( (rnd.Next() %2) != 0)
    				{
    					switch (rnd.Next() %3 )
    					{
    						case 0:
    							AStudent.SetName("Janet");
    							break;
    						case 1:
    							AStudent.SetName("David");
    							break;
    						case 2:
    							AStudent.SetName("Ben");
    							break;
    					}
    				}
    				else
    				{
    					theName = AStudent.GetName();
    				}
    			}
    
    			if(Interlocked.Decrement( ref WorkItemNum) == 0)
    			{
    				Done.Set();
    			}
    		}
    
    		[STAThread]
    		static void Main(string[] args)
    		{
    			int threadNum;
    			Student AStudent = new Student();
    
    			// Queue up 20 work items in the ThreadPool.
    			for (threadNum = 0 ; threadNum <= WorkItemNum -1 ; threadNum++) 
    			{
    				ThreadPool.QueueUserWorkItem(new WaitCallback(AccessClassResource),AStudent);
    			}
    
    			Done.WaitOne();
    			Console.WriteLine("All operations have completed. Press enter to exit");
    			Console.ReadLine();
    		}
    	}
    }
  6. ????????? ?????, ?? ??? ????????? ??????

??????

???? ??????? ?? ???, ????? Microsoft ?????? ??????? (MSDN) ??? ???? ?????:
????? Pooling
(vs.71) http://msdn2.Microsoft.com/en-us/library/0ka9477y .aspx
ReaderWriterLock
(vs.71) http://msdn2.Microsoft.com/en-us/library/bz6sth95 .aspx
ReaderWriterLock ????
(vs.71) http://msdn2.Microsoft.com/en-us/library/SYSTEM.threading.readerwriterlock .aspx

???

???? ID: 816161 - ????? ???????: 30 ??????? 2010 - ??????: 4.0
???? ???? ???? ??:
  • Microsoft Visual C# 2008 Express Edition
  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
??????: 
kbthreadsync kbthread kbhowtomaster kbmt KB816161 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:816161

??????????? ???

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com