Why is DCLP not feasible? (1)

Source: Internet
Author: User

When looking at the POCO network library, a singleton mode is implemented, and the feasibility of DCLP is mentioned. Check it out,
Found this article. The original text is too long, and the meaning is roughly summarized as follows.

Singleton is almost the most common of all design patterns, but it is not thread-safe.
DCLP is designed to eliminate this shortcoming-Double Checked Locking Pattern. But it is still
Unreliable.


The general singleton implementation is:
// From the header file
Class Singleton {
Public:
Static Singleton * instance ();
 
Private:
Static Singleton * pInstance;
};
// From the implementation file
Singleton * Singleton: pInstance = 0;
Singleton * Singleton: instance (){
If (pInstance = 0) {// Line 1
PInstance = new Singleton; // Line 2
}
Return pInstance;
}
This method works well in single-threaded mode. However, there is a problem in the multi-threaded mode.
Suppose thread A enters the instance function, runs to Line 1, and is suspended. When it is suspended, it
PInstance is NULL, so no Singleton object has been created.
Then thread B enters the instance and runs to line1. It finds that pInstance is NULL. Then, execute the next sentence and create
Singleton and point pInstance to it. Then return pInstance.
When thread A continues to execute, it will execute Line2, create A Singleton and point pInstance to it.
Destroys the meaning of singleton because two singleton instances are created.
To ensure thread security, you need to add a lock between the pinstances.
Singleton * Singleton: instance (){
Lock lock;
// Acquire lock (params omitted for simplicity)
If (pInstance = 0 ){
PInstance = new Singleton;
}
Return pInstance;
}
// Release lock

In this case, there is a major drawback: the cost is too high, and a lock is required for each access. But in fact, we only need
Apply a lock when you create the instance for the first time. If
The instance has been called n times, so we only need to lock the instance during the first call. DCLP is used
-------------- Designed to solve this problem, remove unnecessary locks.
Singleton * Singleton: instance (){
If (pInstance = 0 ){
// 1st test
Lock lock;
If (pInstance = 0 ){
// 2nd test
PInstance = new Singleton;
}
}
Return pInstance;
}
DCLP tests whether pInstance is NULL before locking. a lock is required only when pInstance is NULL. The second test is also
It is necessary because another thread may execute new when testing pInstance and requesting LOCK for the first time.

Problem:
PInstance = new Singleton; consists of the following three steps:
1. allocate memory (sizeof (Singleton ).
2. Create a Singleton object on the allocated memory.
3. Point pInstance to the memory. The problem is that the compiler does not necessarily perform these three steps in sequence. Sometimes the compiler will
The second and third steps are exchanged. This situation may be shown below:
Singleton * Singleton: instance (){
If (pInstance = 0 ){
Lock lock;
If (pInstance = 0 ){
PInstance =
// Step 3
Operator new (sizeof (Singleton); // Step 1
// Step 2
New (pInstance) Singleton;
}
}
Return pInstance;
}
In the actual DCLP code, step 2 may throw an exception. In this case, you must ensure that the pInstance is not changed (setp3 is not
Execution ). So it is generally not possible to move step 3 to step 2, but sometimes it is possible. For example, step 2 does not throw an exception.
.
Now, if thread A enters the instance for the first test, a lock is requested, and step1 and step3.
Then it is suspended. In this case, pInstance is NO-NULL, but NO singleton object is created.
Then thread B enters the instance, finds that pInstance is not empty, returns it, And then unreferences it, but there is no object.
Therefore, DCLP works normally only when step 1 and Step 2 are completed before step 3, but C/C ++ does not provide
Yes.


From the soul of joy

Related Article

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.