Ref: The art of Multiprocessor Programming)
Note: I understand it as red (not sure it is correct) and it is black in reference books.
Last modification time: 09-16-2011
----------------------------------------------------------------------------------
Time 2.1
-A thread is a state machine and its state transition is called an event.
-Events occur instantly. We assume that different events occur at different time points.
-When there are overlapping event intervals, it is called concurrency.
2.2 critical section
-Code that can be executed by only one thread at a time is called a critical section.
-The above features are called mutex.
-
+ Mutex: The Critical Zones of different threads do not overlap.
+ No deadlock: If a thread is trying to obtain a lock, the lock will always be successfully obtained.
+ No hunger: Every thread trying to get the lock will eventually succeed. // No hunger means no deadlock.
// The hunger-free feature does not guarantee how long the thread will wait before entering the critical section.
-Even if each lock used by the program meets the deadlock-free feature, the program may also experience deadlocks.
2.3 dual-thread Solution
2.3.1 lockone Algorithm
2.3.2 locktwo Algorithm
2.3.3 Peterson lock
Peterson lock algorithm: // two-thread mutex Algorithm
Class Peterson implements lock {
// Thread-Local index, 0 or 1
Private volatile Boolean [] flag = new Boolean [2];
Private volatile int victim;
Public void lock (){
Int me = threadid. Get ();
Int he = 1-Me;
Flag [Me] = true; // I'm interested
Victim = Me; // you go first
While (flag [he] & victim = Me) {}; // wait
}
Public void unlock (){
Int me = threadid. Get ();
Flag [Me] = false; // I'm not interested
}
}
-The Peterson lock is mutually exclusive and does not support hunger or deadlock.
2.4 filter lock
Algorithm code:
Class filter implements lock {
Int [] level;
Int [] victim;
Public filter (int n ){
Level = new int [N];
Victim = new int [N]; // use 1 .. n-1
For (INT I = 0; I <n; I ++ ){
Level [I] = 0; // [1]
}
}
Public void lock (){
Int me = threadid. Get ();
For (INT I = 1; I <n; I ++) {// attempt Level 1 // [2]
Level [Me] = I;
Victim [I] = me;
// Spin while conflicts exist
While (limit k! = Me) (level [k]> = I & victim [I] = Me) {}; // [3]
}
}
Public void unlock (){
Int me = threadid. Get ();
Level [Me] = 0;
}
}
Note: level [] in the-n integer group, and the value of level [a] indicates the layer that thread a is attempting to enter.
-Each layer has a victim [l] field used to select the thread and "left behind" the layer.
Below is my general explanation of the Code:
[1]: by default, all threads are at Layer 0 and try to enter the first layer.
[2]: I indicates the layer number of the layer that this thread wants to enter.
[3]: (limit k! = Me) (level [k]> = I) indicates that a thread exists on the upper layer of the "Me thread" layer.
If "victim [I] = me" is satisfied, it indicates "no new thread enters this layer ".
My understanding of algorithms ://!!! Assume that the thread that calls lock first enters the critical section.
The first thread that enters the lock will smoothly enter the next layer until it enters the critical section.
Assume that the first thread that enters lock is currently in layer X, and the second thread calls lock.
Then the second thread enters the 1st layer. If no third thread calls lock, the second thread
"Left-behind" will be selected at the first layer. If the third thread calls lock, the second thread
You can enter the second layer. The third thread will "Stay ".
Features that all layers meet:
-At least one thread attempts to enter layer l will succeed.
-If more than one thread needs to enter layer L, at least one thread will be congested at the L-1 layer.
Theorem 2.4.1: for integer J from 0 to n-1, layer J has a maximum of N-J threads.
Inference 2.4.1: the filter lock algorithm satisfies the mutex feature.
Theorem 2.4.2: the filter lock algorithm is free of hunger.
Inference 2.4.2: the lock filtering algorithm has no deadlock.