Discuss some basic concepts of high concurrency (iii) lock

Source: Internet
Author: User

Some basic understanding of the concurrency programming concept is very important. Tell us what we think indicates the main problem in the direction.

Here are some basic concepts about locking.

Under normal circumstances, we are talking about locks that refer to "mutually exclusive" locks. Because there are some special locks, example "read-write lock", there is no mutex at all.

Repel the lock.


A lock is a synchronous means of handling concurrency. The ultimate goal of a single-threaded program and concurrent programs is to ensure the correctness of the program, but the biggest differences are:

    • The correctness of a single-threaded program is only concerned with the implementation of the procedure and the target is consistent
    • Concurrent program Correctness in addition to the implementation of the correct results, but also includes the characteristics of the activity, the so-called activity, refers to the program no deadlock, no hunger

Therefore, to examine a lock, also need to examine from three aspects:

1. Mutual exclusion

2. No deadlock

3. No hunger


The simplest locks are guaranteed to be mutually exclusive, whereas mutual repulsion can be represented by a Boolean value in nature, i.e. a two-dollar state.

Mutual exclusion is a feature that guarantees the correctness of concurrent programs. A special term associated with mutual exclusion is the critical section .

A critical section is a code snippet that "can only be run by one thread at a time", which is a lock-protected piece of code that is usually locked.


The definition of a mutually exclusive lock is usually for example the following

Interface Lock {public     void Lock ();     public void Unlock ();}

The thread must use the lock in the specified manner. The lock action must be called before the try block, assuming that lock is running inside a try. An exception may be thrown before the lock is taken, causing the unlock action to run and the error to occur.

Familiar with the Java display lock students must know that the use of Reentrylock is for example, the following methods of use.

Mutex.lock (); try{... Critical section}finally{    Mutex.unlock ()}

Mutual exclusion means serial, also means waiting. This leads to the famous Amdahl law.

Amdahl law: That is, the completion of a work can be achieved by the speedup, limited by the work must be serial part.

(usually the serial part is protected by a mutually exclusive lock)

The speedup is defined as the time at which a processor finishes a work and the time it takes to complete the work with N processors concurrently.

The acceleration of the Amdahl law is given by the analogy of S = 1/(1-p + P/N) s for the time of the completion of the acceleration ratio of 1 p refers to the number of parallel parts n refers to a processor

From the Amdahl law, it can be seen that the more serial work, the smaller the speedup is obtained.

Amdahl gives us practical inspiration for programming:

1. minimize the particle size of the mutually exclusive lock . Smaller lock granularity means fewer serial parts

2. Do not use locks without locks. No lock means less serial parts


Next, the concept of activity-related.


Deadlock means that the system freezes . finally all the related threads are permanently stalled waiting.

Hunger is always some thread that can be executed , a small number of threads permanently stuck waiting


So no hunger means there must be no deadlock. But no deadlock does not mean that there is no hunger.


The art of multiprocessor programming gives the implementation of several locks, in which the Peterson algorithm ensures that locks are mutually exclusive and deadlock-free when two threads use locks. No hunger characteristics.

Class Peterson implements Lock {     private boolean[] flag = new BOOLEAN[2];     private int victim;     public void Lock () {           int i = Threadid.get ();           int j = 1-i;           flag[i]= true;  Two threads are guaranteed to execute the deadlock successively, realizing mutually rejecting            victim = i;  Guaranteed two threads do not deadlock at the same time, achieve mutual exclusion while           (flag[j] && victim = = i) {}  //Mutually exclusive means wait     } public     void Unlock () {           int i = Threadid.get ();           Flag[i] = false;    }}

The bakery lock supports the mutual exclusion protocol for n threads.

Mathematically proven:

N-Thread's deadlock-free algorithm requires n different storage units (variables) to hold the intermediate state.






Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Discuss some basic concepts of high concurrency (iii) lock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.