Csharp/CSharp Tutorial/Thread/LocalDataStoreSlot

Материал из .Net Framework эксперт
Версия от 15:20, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Thread-local-storage: Named slots

<source lang="csharp">using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Reflection; using System.Runtime; using System.Runtime.rupilerServices; using System.Security; using System.Text; public class MainClass {

   public static void Main()
   {
       Thread.SetData(Thread.AllocateNamedDataSlot("TestSlot"), 126);
       int slotValue2 = (int)Thread.GetData(Thread.GetNamedDataSlot("TestSlot"));
       Console.WriteLine(slotValue2);
   }

}</source>

126

Thread-local-storage: Unnamed slots

<source lang="csharp">using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Reflection; using System.Runtime; using System.Runtime.rupilerServices; using System.Security; using System.Text; public class MainClass {

   public static void Main()
   {
       LocalDataStoreSlot slot = Thread.AllocateDataSlot();
       Thread.SetData(slot, 63);
       int slotValue1 = (int)Thread.GetData(slot);
       Console.WriteLine(slotValue1);
   }

}</source>

63

Thread-static local-storage

<source lang="csharp">using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Reflection; using System.Runtime; using System.Runtime.rupilerServices; using System.Security; using System.Text; using System.Threading; public class MainClass {

   [ThreadStatic]
   private static string threadStaticData = "Empty";
   public static void Main()
   {
       Thread[] threads = new Thread[3];
       for (int i = 0; i < 3; i++)
       {
           threads[i] = new Thread(delegate(object j) {
               threadStaticData = "thread no: " + j;
               Console.WriteLine("[Thread{0}] = {1}", j, threadStaticData);
           });
           threads[i].Start(i);                
       }
       foreach (Thread t in threads)
           t.Join();
       Console.WriteLine("[Master] after loop = {0}", threadStaticData);
   }

}</source>

[Thread0] = thread no: 0
[Thread1] = thread no: 1
[Thread2] = thread no: 2
[Master] after loop = Empty

Use LocalDataStoreSlot

<source lang="csharp">/* Book Accelerated C# 2005

   * By Trey Nash
   * ISBN: 1-59059-717-6
   * 432 pp.
   * Published: Aug 2006
   * Price: $39.99
  • /

using System; using System.Threading; public class MyClass {

   static MyClass() {
       tlsSlot = Thread.AllocateDataSlot();
   }
   public MyClass() {
       Console.WriteLine( "Creating MyClass" );
   }
   public static MyClass MyThreadDataClass {
       get {
           Object obj = Thread.GetData( tlsSlot );
           if( obj == null ) {
               obj = new MyClass();
               Thread.SetData( tlsSlot, obj );
           }
           return (MyClass) obj;
       }
   }
   private static LocalDataStoreSlot tlsSlot = null;

} public class MainClass {

   private static void ThreadFunc() {
       Console.WriteLine( "Thread {0} starting...",Thread.CurrentThread.GetHashCode() );
       Console.WriteLine( "tlsdata for this thread is \"{0}\"", MyClass.MyThreadDataClass );
       Console.WriteLine( "Thread {0} exiting", Thread.CurrentThread.GetHashCode() );
   }
   static void Main() {
       Thread thread1 = new Thread( new ThreadStart(ThreadFunc) );
       Thread thread2 = new Thread( new ThreadStart(ThreadFunc) );
       thread1.Start();
       thread2.Start();
   }

}</source>

Thread 3 starting...
Creating MyClass
tlsdata for this thread is "MyClass"
Thread 3 exiting
Thread 4 starting...
Creating MyClass
tlsdata for this thread is "MyClass"
Thread 4 exiting

Use thread-local storage

<source lang="csharp">using System; using System.Threading; class MainClass {

 public static void SetError() 
 {
   Random r = new Random();
   Thread.SetData(Thread.GetNamedDataSlot("Number"), r.Next(100));
   Thread.SetData(Thread.GetNamedDataSlot("Name") ,Thread.CurrentThread.Name);
   Console.WriteLine("Number = " + Thread.GetData(Thread.GetNamedDataSlot("Number")));
   Console.WriteLine("Name = " + Thread.GetData(Thread.GetNamedDataSlot("Name")));
 }
 public static void Main() 
 {
   Thread.AllocateNamedDataSlot("Number");
   Thread.AllocateNamedDataSlot("Name");
   Thread t2 = new Thread(new ThreadStart(SetError));
   t2.Name = "t2";
   t2.Start();
   Thread t3 = new Thread(new ThreadStart(SetError));
   t3.Name = "t3";
   t3.Start();
   Thread.FreeNamedDataSlot("Number");
   Thread.FreeNamedDataSlot("Name");
 }

}</source>

Number = 81
Name = t2
Number = 81
Name = t3