Thread basics of thread synchronization

First, what is the thread synchronization

  1: Thread Synchronization technology is typically used as a shared resource of competitive conditions we encounter multiple threads using a single object caused, how to take the correct order to perform what he called thread synchronization when multiple threads

  2: Kernel mode, when the thread occupies a shared object, so that other threads in a blocked state may cause context switching (OS thread scheduler save waiting threads, and switch to another thread, the thread returns to the standby sequence.)

        Performance will consume

  3: a user mode, waiting for a simple, does not drop to the blocked state thread switch, cpu time wasted waiting for the thread, but the context switch saves cpu time consuming, this mode of light, fast, short thread for

  4: Mixed mode: first try to user mode, if a long time to switch the kernel mode to save cpu resources

Second, why the use of thread synchronization

  1: Thread Synchronization can be shared by multiple threads to solve a complex problem objects competition deadlock caused

Third, how to use thread synchronization

  1: atomic operation using threads (one thread only takes a quantum of time, time to complete) no other thread is waiting for the current operation wanc, to avoid the use of locks, thus avoiding the deadlock, thereby blocking the thread can not avoid competition condition

. 1  the using the System;
 2  the using the System.Threading;
 . 3  the using  static the System.Console;
 . 4  
. 5  namespace Chapter2.Recipe1
 . 6  {
 . 7      Internal  class Program
 . 8      {
 . 9          Private  static  void the Main ( String [] args)
 10          {
 . 11              the WriteLine ( " here Incorrect counter " );
 12              // negative example: create a thread calls TestCounter three methods to perform increment decrement, the thread is unsafe, will result in competitive conditions, will be run many times the result is not a non-zero 
13             var c = new Counter();
14             var t1 = new Thread(() => TestCounter(c));
15             var t2 = new Thread(() => TestCounter(c));
16             var t3 = new Thread(() => TestCounter(c));
17             t1.Start();
18             t2.Start();
19             t3.Start();
20             t1.Join();
21             t2.Join();
22             t3.Join();
23 
24             WriteLine($"COUNT the Total: c.Count {} " );
 25  
26 is              the WriteLine ( " Correct counter " );
 27              // positive examples: Interlocked provide atomic operations using a plurality of shared variables threads 
28              var C1 = new new CounterNoLock ();
 29  
30              T1 = new new the Thread (() => TestCounter (C1));
 31 is              T2 = new new the Thread (() => TestCounter (C1));
 32              T3 = new new the Thread (() => TestCounter (C1));
 33 is              T1. the Start ();
 34 is              t2.Start ();
35             t3.Start();
36             t1.Join();
37             t2.Join();
38             t3.Join();
39 
40             WriteLine($"Total count: {c1.Count}");
41             ReadLine();
42         }
43         
44         static void TestCounter(CounterBase c)
45         {
46             for (int i = 0; i < 100000; i++)
47             {
48                 c.Incrementa();
49                 c.Decrement();
50             }
51         }
52         //反面例子 
53         class Counter : CounterBase
54         {
55             private int _count;
56 
57             public int Count => _count;
58 
59             public override void Incrementa()
60             {
61                 _count++;
62             }
63 
64             public override void Decrement()
65             {
66                 _count--;
67             }
68         }
69 
70         class CounterNoLock : CounterBase
71         {
72             private int _count;
73 
74             public int Count => _count;
75 
76             public override void Incrementa()
77             {
78                 Interlocked.Increment(ref _count);
79             }
80 
81             public override void Decrement()
82             {
83                 Interlocked.Decrement(ref _count);
84             }
85         }
86 
87         abstract class CounterBase
88         {
89             public abstract void Incrementa();
90 
91             public abstract void Decrement();
92         }
93     }
94 }
Atomic operations

 

Guess you like

Origin www.cnblogs.com/LZXX/p/11858157.html