This is regarding the article titled "C++ & Double-Checked Locking" by
Scott Meyers and Andrei in DDJ July 2004 issue.
I think the reasoning in this article is fundamentally flawed due the
authors mixing up a couple of concepts (observable behavior & side
effects) that are defined separately in the standard.
Background: In the following statement...
class Singelton {
Singleton* pinstance;
public:
Singleton* instance() {
..
pinstance = new Singelton( ); // whats going on here ?
..
}
};
The assignment statement involves 3 steps. 1) allocate mem for object,
2) invoke constructor 3) assign address of object to pointer
The problem that the authors note is that within the rules of the
standard steps 2 and 3 can be executed in any order after step 1. This
is may be true as I cant find a good "sequence point" (SP) [defined in
1.9.7] that will disallow this. Yes there are 3 SPs here. One at start
of object's constructor, one at the end of the object's construtor and
one at the end of the assignment statement. But with just these 3 SPs I
am unable to make a solid case for strict ordering of 1,2 and 3. (the
authors fail to mention the first 2 SPs )
Now the authors claim that there is no way for the programmer to
express, in C or C++, the strict ordering that step 3 should follow step
2. I dont think this is the case. here is a trivial solution..
void Singleton::assi gn( Singleton * obj)
{
pinstance = obj;
}
Singleton* instance() {
..
assign ( new Singelton( ) ) ; // we introduce a SP before
assignment can occur
..
}
What I have done here is simply introduce a SP that will ensure the
correct ordering. i.e introduce a SP just after step 2 and right before
step 3.
At the heart of the matter is what the Scott writes to me in a pvt email
"As we tried to point out in the article, sequence points don't offer
very much help with this problem."
This is incorrect (I feel) as demonstrated by the solution above.
Now here is some standardese to back me up.....
1.9.6 // Definition of observable behavior
The observable behavior of the abstract machine is its sequence of reads
and writes to volatile data and
calls to library I/O functions.
1.9.7 // Definition of side effects and SP
Accessing an object designated by a volatile lvalue (3.10), modifying an
object, calling a library I/O
function, or calling a function that does any of those operations are
all side effects, which are changes in the
state of the execution environment. Evaluation of an expression might
produce side effects. At certain
specified points in the execution sequence called sequence points, all
side effects of previous evaluations
shall be complete and no side effects of subsequent evaluations shall
have taken place.7)
1.9.12
A fullexpression is an expression that is not a subexpression of another
expression.
1.9.16
There is a sequence point at the completion of evaluation of each
fullexpression
1.9.17
When calling a function (whether or not the function is inline), there
is a sequence point after the evaluation
of all function arguments (if any) which takes place before execution of
any expressions or statements in
the function body. There is also a sequence point after the copying of a
returned value and before the execution
of any expressions outside the function11).
also 1.9.18 may be of interest but is not relevant for my argument.
Footnote:
All that said.. I am still not fully convinced that steps 2 and 3 can
execute in any order. Reasoning : Order of evaluation of assignment
operator is from right to left. So the "new expression" has to evaluated
completely first. But things get fuzzy as there is no "solid" SP here to
disallow the compiler from reordering. I am not sure what role the SPs
at the start and end of obect's contructor would play (if any) in
defining the strict ordering of the steps 2 and 3. 5 1978
Roshan wrote: This is regarding the article titled "C++ & Double-Checked Locking" by Scott Meyers and Andrei in DDJ July 2004 issue.
I think the reasoning in this article is fundamentally flawed due the authors mixing up a couple of concepts (observable behavior & side effects) that are defined separately in the standard.
(snip) Footnote: All that said.. I am still not fully convinced that steps 2 and 3 can execute in any order. Reasoning : Order of evaluation of assignment operator is from right to left. So the "new expression" has to evaluated completely first. But things get fuzzy as there is no "solid" SP here to disallow the compiler from reordering. I am not sure what role the SPs at the start and end of obect's contructor would play (if any) in defining the strict ordering of the steps 2 and 3.
There's no way to discuss this with respect to threading since the C++
standard does not take threading into account. The standard is only
meaningful for a single thread.
For C, POSIX compliance defines additional requirements that make C
work for POSIX functions. For other synchronization not defined by
POSIX, other techniques are used to create the right effect. SP's
are only part of it. For more on DCL see here http://www.cs.umd.edu/~pugh/java/mem...edLocking.html
There's one or two patterns that might help depending on what you
are doing. But you sort of have to be aware of the problem with
DCL to realize that you should resort to an alternate solution.
Joe Seigh There's no way to discuss this with respect to threading since the C++ standard does not take threading into account. The standard is only meaningful for a single thread.
I never mention anything about multithreaded programs. Everything that I
have i said above is applicable
to single threaded programs ! And this does not even involve DLCP per say..
Its just about restrictions on
srict ordering of instructions(in a single thread of execution), sequence
points, side effects and observable behavior.
DLCP and the problems associated with comes later on...once you understand
instruction sequencing guarantees correctly.
Roshan wrote:
Don't multi-post. I never mention anything about multithreaded programs. ...
"As if" rule. Whatever the standard says, transformations that
can't break a single-threaded program are legal (and desirable).
More can be found in the reply on c.s.c++.
regards,
alexander.
On Thu, 10 Jun 2004 19:44:20 GMT, Roshan <ro****@yahoo.c om> wrote: This is regarding the article titled "C++ & Double-Checked Locking" by Scott Meyers and Andrei in DDJ July 2004 issue.
I haven't read the article, but I have just read this: http://www.nwcpp.org/Downloads/2004/DCLP_notes.pdf
which I imagine is very similar.
I think the reasoning in this article is fundamentally flawed due the authors mixing up a couple of concepts (observable behavior & side effects) that are defined separately in the standard.
Background: In the following statement...
class Singelton { Singleton* pinstance; public: Singleton* instance() { .. pinstance = new Singelton( ); // whats going on here ? .. } };
The assignment statement involves 3 steps. 1) allocate mem for object, 2) invoke constructor 3) assign address of object to pointer
The problem that the authors note is that within the rules of the standard steps 2 and 3 can be executed in any order after step 1. This is may be true as I cant find a good "sequence point" (SP) [defined in 1.9.7] that will disallow this. Yes there are 3 SPs here. One at start of object's constructor, one at the end of the object's construtor and one at the end of the assignment statement. But with just these 3 SPs I am unable to make a solid case for strict ordering of 1,2 and 3. (the authors fail to mention the first 2 SPs )
Ok.
Now the authors claim that there is no way for the programmer to express, in C or C++, the strict ordering that step 3 should follow step 2. I dont think this is the case. here is a trivial solution..
void Singleton::assi gn( Singleton * obj) { pinstance = obj; }
Singleton* instance() { .. assign ( new Singelton( ) ) ; // we introduce a SP before assignment can occur ..
There's a far more trivial solution:
Singleton* temp = new Singleton(); //sequence point here.
pinstance = temp;
(Note they use this example later).
I really don't think the authors are trying to say there is no way of
introducing a sequence point after the construction of the singleton
and before the assignment to the instance variable.
}
What I have done here is simply introduce a SP that will ensure the correct ordering. i.e introduce a SP just after step 2 and right before step 3.
At the heart of the matter is what the Scott writes to me in a pvt email
"As we tried to point out in the article, sequence points don't offer very much help with this problem."
This is incorrect (I feel) as demonstrated by the solution above.
Huh!? The problem in question involves writing correct lazy evaluation
using DCL. How does introducing a sequence point help this?
Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
What you said was absolutely correct regarding a single-threaded
program if you don't drag DCL into this issue.
However, DCL is about multi-threading and Sequence Point is somewhat
irrelevant for it. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: higabe |
last post by:
Three questions
1)
I have a string function that works perfectly but according to W3C.org
web site is syntactically flawed because it contains the characters </
in sequence. So how am I supposed to write this function?
String.replace(/</g,'<');
|
by: Jon Skeet [C# MVP] |
last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the
article on all the groups this is posted to.
I've finally managed to finish my article on multi-threading - at least
for the moment. I'd be *very* grateful if people with any interest in
multi-threading would read it (even just bits of it - it's somewhat
long to go through the whole thing!) to check for accuracy,
effectiveness of examples, etc.
Feel free to mail...
|
by: Nick Bolton |
last post by:
Hello,
Maybe a common case, but I really want to not so much as migrate from
PHP/MySQL to C# .NET but learn the new language.
I have made maybe 3/4 very simple systems using C# .NET, but they all
seem to be a little, well simple -- and not uniform at all.
When I use PHP, I always incorperate some kind of template system, so,
say I have a master template and a child template which is loaded
|
by: Terry Olsen |
last post by:
How do I loop back to the beginning of a for/next loop before getting to
the end of it? Isn't there an "iterate" command or something like that?
For Each This in That
...code
if This = False Then (start loop over with next This)
...code
Next
*** Sent via Developersdex http://www.developersdex.com ***
|
by: Brian |
last post by:
An MSKB article on the scalability of ADO/ASP
(http://support.microsoft.com/kb/176056/EN-US/) says in a discussion of
why connection objects shouldn't be stored in session variables, "If
you do not pool there will be idle connections wasting server and
network resources. You also have some threading issues that can occur
if multiple concurrent threads end up hitting on the same connection
(though the session ID might save you here, but it...
| |
by: m.banaouas |
last post by:
Hi,
bonjour,
witch versions are suitable to use for apache & mod_python ?
Can i install and use "Apache 2.2.3" & "mod_python 3.2.10" (most recent
versions) without facing any known major issue ?
thanks for any help.
|
by: Gigs_ |
last post by:
can someone explain me this code:
----------------------------------
import thread
stdoutmutex = thread.allocate_lock()
exitmutexes = * 10
def counter(myId, count):
for i in range(count):
stdoutmutex.acquire()
print ' =%s' % (myId, i)
|
by: dragoncoder |
last post by:
Hi everyone, please consider the following function:-
const int& foo ( const double& d )
{
return d;
}
g++ compiles it with warnings and solaris CC gives error. I want to
know if the code is correct according to the standard ?
|
by: Ollie Riches |
last post by:
I'm looking into a production issue related to a windows service and
System.Timers.Timer. The background is the windows service uses a
System.Timers.Timer to periodically poll a directory location on a
network for files and then copies these files to another location (on
the network) AND then updates a record in the database. The file
copying is performed before the database update because the file
system is not transactional.
The code...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |