473,378 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Double-checked locking: is this alternative OK?

SomeObject* instance() // flawed way
{
static SomeObject* instance = NULL;
LockThing(); // locking even when not required
if(instance == NULL) // instance may still be NULL when another thread
checks
instance = new SomeObject();
UnlockThing(); // oh dang, more unnecessary function calls after first
pass
return instance;
}

SomeObject* instance() // OK?
{
static bool been_there_done_that = LockThing();
static volatile instance = new SomeObject(); // no problemo (on X86
hardware)
static bool been_there_done_that_B = UnlockThing();
return instance; // fast is nice! No overhead after first pass through
function
}

John
Mar 17 '07 #1
3 1872
JohnQ wrote:
SomeObject* instance() // flawed way
{
static SomeObject* instance = NULL;
LockThing(); // locking even when not required
if(instance == NULL) // instance may still be NULL when another
thread checks
instance = new SomeObject();
UnlockThing(); // oh dang, more unnecessary function calls after
first pass
return instance;
}
Could you explain why you don't do it this way:

if (instance == NULL) {
LockThing();
instance = new SomeObject();
UnlockThing();
}
return instance;

? That should avoid the unnecessary locking you mention, no?
>
SomeObject* instance() // OK?
{
static bool been_there_done_that = LockThing();
static volatile instance = new SomeObject(); // no problemo (on X86
hardware)
static bool been_there_done_that_B = UnlockThing();
return instance; // fast is nice! No overhead after first pass
through function
}

John

The problem with having to double-check is not solved by your static
initialisation, however. The language at this point makes no attempt
to resolve the problem of calling the function "for the first time"
from two different threads. And AIUI the sequence of events in that
case can be

Thread 1 Thread 2
LockThing() - succeeds
LockThing() - fails, waits
new SomeObject() . // waiting
UnlockThing() . // waiting
return instance
new SomeObject() // overrides instance
UnlockThing()
return instance // different instance

The Committee is working on thread support, however. Static
initialisations would have to be resolved before the thread support
can be claimed, that's for sure.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 17 '07 #2
On Mar 17, 8:31 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
if (instance == NULL) {
LockThing();
instance = new SomeObject();
UnlockThing();
}
return instance;
I think this can be improved to:

if (instance == NULL) {
LockThing();
if (instance == NULL) {
instance = new SomeObject();
}
UnlockThing();
}
return instance;

This is mentioned in [1] as "The Double-Checked Locking Pattern."
Thread 1 Thread 2
LockThing() - succeeds
LockThing() - fails, waits
new SomeObject() . // waiting
UnlockThing() . // waiting
return instance
new SomeObject() // overrides instance
The above code fixes this, instead of calling "new SomeObject()":
Thread 1 Thread 2
Checks if instance is NULL
(Since instance is not NULL,
do *not* call "new SomeObject()")
UnlockThing()
return instance // different instance
[1] "Modern C++ Design" Andrei Alexandrescu

Mar 21 '07 #3
On Mar 20, 11:53 pm, "david.baird" <dhba...@gmail.comwrote:
On Mar 17, 8:31 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
if (instance == NULL) {
LockThing();
instance = new SomeObject();
UnlockThing();
}
return instance;

I think this can be improved to:

if (instance == NULL) {
LockThing();
if (instance == NULL) {
instance = new SomeObject();
}
UnlockThing();
}
return instance;
Okay, I read my book a little bit more and found that actually even
this double-checked solution can have problems in SMP systems where
memory writes are committed in non-chronological bursts. Using the
"volatile" keyword might help and there might also be special
concurrency features offered by your platform.

Mar 21 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pepijn Kenter | last post by:
Dear experts. I have a vector<float> and want to convert that to a vector<double>. I optimistically tried: #include <vector> #include <iostream> using namespace std; int main() {
12
by: Sydex | last post by:
When I compile code I get error C2664: 'Integration::qgaus' : cannot convert parameter 1 from 'double (double)' to 'double (__cdecl *)(double)' in this part : double Integration::quad2d(double...
13
by: maadhuu | last post by:
hello , i would like to know as to why double is more efficient than float . thanking you, ranjan.
12
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
52
by: lcw1964 | last post by:
Greetings, all, I am trying to port a little bit of math code to gcc, that in the original version used the long double version of several functions (in particular, atanl, fabsl, and expl). I...
2
by: dj10fld | last post by:
I am getting a (cannot convert double to double in assignment errors) here is a part of my code #include <iostream> #include <iomanip> #include <cmath> using namespace std; #define MaxSize...
2
by: xelloss | last post by:
#include<cstdlib> #include<iostream> #include<iomanip> #include<vector> #include<fstream> using namespace std; double sum(vector<double> x) { double total = 0.0;
1
by: krishna81m | last post by:
I am a newbie and have been trying to understand conversion from double to int and then back to int using the following code was posted on the c++ google group. Could someone help me out with...
10
by: ratcharit | last post by:
Currently using cosine function in math.h Currently I get: 1 = cos(1e^-7) Is there another way for cos to return value of high accuracy say: 0.999999 = cos(1e^-7)
2
by: Genro | last post by:
#include<stdio.h> #include<TX/graphics.h> #include<time.h> // I need help! struct Krug{ double _x; double _y; double _skox; double _skoy; double...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.