Multi-threaded programming with respect to a single thread will be a unique problem, thread-safety issues. The so-called security thread is that if there are multiple threads in a process where your code at run simultaneously, and these threads may run this code at the same time. If the result of each run single-threaded operating results and the same, and also the values of other variables and expected the same. Thread safety problems are caused by the global variables and static variables.
In order to ensure multiple threads, secure access static variables, you can use the lock mechanism to ensure, as follows:
1 // need to lock the static global variable 2 Private static BOOL _isOK = to false ; . 3 // Lock can lock a reference type variable . 4 Private static Object _lock = new new Object (); . 5 static void mlock () . 6 { . 7 / / multithreading . 8 new new the System.Threading.Thread (the Done) .Start (); . 9 new new the System.Threading.Thread (the Done) .Start (); 10 Console.ReadLine (); . 11 } 12 is 13 is static void the Done () 14 { 15 // Lock can lock a reference type variable 16 Lock (_lock) . 17 { 18 is IF (! _isOK) . 19 { 20 is Console.WriteLine ( " the OK " ); 21 is _isOK = to true ; 22 is } 23 is } 24 }
Alone this lock out for the next
private static object _lock = new object();
At first I just write object locker = new object ();
After reading the online code, I looked down, basically private static readonly so I guest at Baidu
private: If the instance is public, so independent code may also lock the object, it will appear the same object multiple threads wait for the release, resulting in deadlock
static: as above, if the data type is a common problem also occurs above
readonly: Once the lock in the value of the object has changed, so other threads can come in to perform, because the mutex object has changed
Note that, Lock can lock an object type references. Further, in addition to the locking mechanism, the high version of C # and added to the async await method to ensure thread safety, as follows:
public static class AsynAndAwait { // STEP. 1 Private static int count = 0 ; // with async and await a static variable multithreaded ensure security count public async static void Ml () { // async and await a plurality of serially threads processing // wait until after the statement await execution completion // before performing other statements of this thread // STEP 2 await Task.Run ( new new the Action (M2)); Console.WriteLine ( " Current iS the thread ID {0} ", System.Threading.Thread.CurrentThread.ManagedThreadId); //step 6 count++; //step 7 Console.WriteLine("M1 Step is {0}", count); } public static void M2() { Console.WriteLine("Current Thread ID is {0}", System.Threading.Thread.CurrentThread.ManagedThreadId); //step 3 System.Threading.Thread.Sleep(3000); //step 4 count++; //step 5 Console.WriteLine("M2 Step is {0}", count); } }
In the timing chart, we can see, there are two threads interact, as shown below:
After async and await, the execution order of the above code is shown below:
If each thread to global variables, static variables only read, no write operation, in general, the global variable is thread-safe; if multiple threads simultaneously read or write to a variable, generally need to be considered thread synchronization, otherwise it may affect the thread safety.