I did not expect my first two articlesArticleIt is quite popular. Thank you for choosing mutex today.
I. mutex
First, let's take a look at msdn's explanation:
Yes, there is a bright spot that can be used for "inter-process synchronization". Since both processes can be synchronized, isn't thread synchronization a piece of cake for it? Okay. Check whether mutex is in
The magical effect of the thread.
1: synchronization between threads
Metux provides watione and releasemutex to ensure that there is only one thread to access Shared resources. Is it similar to monitor? Let me give a simple example,
Note that I did not name metux.
1 Class Program
2 {
3 Static Void Main ( String [] ARGs)
4 {
5 For ( Int I = 0 ; I < 20 ; I ++)
6 {
7 Thread t = New Thread (run );
8
9 T. Start ();
10 }
11
12 Console. Read ();
13 }
14
15 Static Int Count = 0 ;
16
17 Static mutex = new mutex ();
18
19 Static Void Run ()
20 {
21 Thread. Sleep ( 100 );
22
23 Mutex. waitone ();
24
25 Console. writeline ( " Current number: {0} " , ++ Count );
26
27 Mutex. releasemutex ();
28 }
29 }
2: Process Synchronization
This time I give mutex a name named cnblogsProgramCopy one copy, and then see if the process synchronization can be realized?
1 Class Program
2 {
3 Static Void Main ( String [] ARGs)
4 {
5 Thread t = New Thread (run );
6
7 T. Start ();
8
9 Console. Read ();
10 }
11
12 Static mutex = new mutex (false, "cnblogs ");
13
14 Static Void Run ()
15 {
16 Mutex. waitone ();
17
18 Console. writeline ( " Current Time: {0} I am a thread: {1}. I have already entered the critical section. " , Datetime. Now, thread. currentthread. gethashcode ());
19
20 // 10 s
21 Thread. Sleep ( 10000 );
22
23 Console. writeline ( " \ N current time: {0} I am a thread: {1}. I am going to exit the critical section. " , Datetime. Now, thread. currentthread. gethashcode ());
24
25 Mutex. releasemutex ();
26 }
27 }
3: Summary
①: Processes can be synchronized when mutex is named, and threads can be synchronized without naming. For details, refer to msdn:
②: Mutex encapsulates the Win32 synchronization mechanism, while monitor is encapsulated by the Framework. Therefore, in terms of thread synchronization, monitor is shorter and more refined than mutex. if the process is implemented
Synchronization does not work on monitor, so mutex is the first choice.
Ii. Interlocked
Ask msdn for an explanation first.
"Atomic operation" is a bright spot. We know that "Atomic" cannot be further divided. A deeper point means that manual intervention is not required from the programmer's perspective, that is, the so-called "lock-free programming ".
In practical applications, sometimes we may just perform some simple operations on shared variables, such as "auto-increment, auto-subtraction, sum, value assignment, and comparison ".
1: Increment
It is interesting to see if the auto-increment effect cannot be further divided.
Class Program
{
Static Void Main ( String [] ARGs)
{
For ( Int I = 0 ; I < 20 ; I ++)
{
Thread t = New Thread (run );
T. Start ();
}
Console. Read ();
}
Static Int Count = 0 ;
Static Mutex = New Mutex ();
Static Void Run ()
{
Thread. Sleep ( 100 );
Console. writeline ("current number: {0}", interlocked. increment (ref count ));
}
}
2: Decrement
This does not need to be used as an example.
3: add
The msdn explanation is quite detailed.
1Static VoidMain (String[] ARGs)
2{
3IntI =10;
4
5Interlocked. Add (RefI,20);
6
7Console. writeline (I );//I = 30
8}
4: Exchange
This is the so-called Atomic value assignment operation.
1Static VoidMain (String[] ARGs)
2{
3IntI =10;
4
5Interlocked. Exchange (RefI,30);
6
7Console. writeline (I );//I = 30
8}
5: compareexchange
Let's look at the Classic msdn statement for the comparison operation.
If they are equal, the second parameter value is returned:
Static VoidMain (String[] ARGs)
{
IntI =10;
Interlocked. compareexchange (RefI,30,10);
Console. writeline (I );//I = 30
}
If they are not equal, the original value is returned:
1 static void main ( string [] ARGs)
2 {< br> 3 int I = 10 ;
4
5 interlocked. compareexchange ( ref I, 30 , 100 );
6
7 console. writeline (I); /// I = 10
8 }