comp.lang.ada
 help / color / mirror / Atom feed
From: Maciej Sobczak <see.my.homepage@gmail.com>
Subject: Re: Robert Dewar's great article about the Strengths of Ada over other langauges in multiprocessing!
Date: Wed, 12 Mar 2008 09:28:42 -0700 (PDT)
Date: 2008-03-12T09:28:42-07:00	[thread overview]
Message-ID: <33557da4-e4ba-4c75-9a55-4b7eb5fdad3b@i12g2000prf.googlegroups.com> (raw)
In-Reply-To: c35feae6-724e-43c3-9871-5462f2844542@b64g2000hsa.googlegroups.com

On 12 Mar, 15:41, gp...@axonx.com wrote:

> > In C++ destructor can be inlined *always*, unless the object is
> > deleted via pointer to base. In other words, objects with automatic or
> > static storage duration (local, static and global) objects can have
> > inlined destructors - no matter whether it is virtual or not.
>
> Yes if compiler can make that determination at compile time.

Certainly the compiler knows at compile time whether the object is a
local variable, static or global. This property does not depend on
anything at run-time.
Example:

class MyClass {/* ... */};
void swallow(MyClass * p) { delete p; }

MyClass G; // global (static in disguise)
void foo()
{
    static MyClass S; // static
    MyClass L; // local

    MyClass * D = new MyClass; // dynamic
    swallow(D);
}

Objects G, S and L can have destructors inlined. This is known at
compile time.
Object denoted by pointer D will most likely have the destructor
called virtually (because function swallow cannot assume anything
about its dynamic type), unless the compiler+linker is smart enough to
do some wider optimizations.

In the most extreme case (global linker optimization), all calls can
be resolved statically.


> Definition of volatile:
[...]

I don't know where you got that definition from, but certainly not
from the standard - and as such it is incorrect.


> Consider the multi-core execution of two concurrent threads.  One will
> modify a variable, another will read.  One will keep the variable in
> the register, another will reload it from the memory.  Volatile will
> force flushing to the memory. Otherwise thread 2 will read erroneous
> data.

No. Your perception of what and where is the "memory" is completely
different from that of CPU(s). The volatile keyword can, at best,
force the compiler to use some "memory address" for access. The
problem is that between "memory address" and "physical memory" there
is a long chain of funny things like asynchronous memory writer in CPU
and a few layers of cache with complicated prefetch functionality. The
cache is transparent so can be ignored, but the memory writer module
and prefetch work *asynchronously* and this means that when you "read"
and "write" your variables, you really don't read and write what you
think - or rather not *when* you think. This happens out of the
compiler's control (it's hardware), so "volatile" cannot help you -
you can as well write it in assembly and still have the same problem.

You need memory barrier to ensure visibility between CPUs and volatile
does not provide it (unless you have some funny compiler). To actually
get the barrier, you have to either apply it by hand or use dedicated
system services that do it for you (mutexes, etc.). Now, the best part
- once you do it, volatile give you nothing.

No, the best part is this: volatile not only gives you nothing, it
actually *slows the program down*, because it prevents the compiler
from applying some obvious optimizations when the given object is used
many times within the critical section.

You don't want to slow your program down, do you? :-)

And last but not least - have you tried to use volatile with some C++
classes, like string, vector, map, etc.? Try it. It will not even
compile, but certainly it is possible to use these classes with many
threads.


Short version: don't use volatile for multithreading. It's not the
correct tool to do the job. The correct ones are membars and these are
part of dedicated system services. Use them.

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com



  parent reply	other threads:[~2008-03-12 16:28 UTC|newest]

Thread overview: 96+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-08  6:04 Robert Dewar's great article about the Strengths of Ada over other langauges in multiprocessing! ME
2008-03-08 22:11 ` Maciej Sobczak
2008-03-09  1:09   ` Christopher Henrich
2008-03-09 13:52     ` Maciej Sobczak
2008-03-09  1:51   ` Phaedrus
2008-03-09  3:17     ` Jeffrey R. Carter
2008-03-09 13:59     ` Maciej Sobczak
2008-03-09  3:15   ` Jeffrey R. Carter
2008-03-09 13:32     ` Maciej Sobczak
2008-03-09 14:02       ` Dmitry A. Kazakov
2008-03-09 18:26       ` Phaedrus
2008-03-10  0:04         ` Ray Blaak
2008-03-10  7:49           ` Georg Bauhaus
2008-03-10 16:48             ` Ray Blaak
2008-03-10  7:53           ` Phaedrus
2008-03-09 22:31       ` Jeffrey R. Carter
2008-03-10  3:53         ` gpriv
2008-03-10  3:04       ` Robert Dewar's great article about the Strengths of Ada over Larry Kilgallen
2008-03-10  9:23         ` Maciej Sobczak
2008-03-10 19:01           ` Jeffrey R. Carter
2008-03-10 22:00             ` Maciej Sobczak
2008-03-11  0:48               ` Jeffrey R. Carter
2008-03-11  7:12                 ` Pascal Obry
2008-03-11  8:59                 ` Maciej Sobczak
2008-03-11  9:49                   ` GNAT bug, Assert_Failure at atree.adb:2893 Ludovic Brenta
2008-03-14 20:03                   ` Robert Dewar's great article about the Strengths of Ada over Ivan Levashew
2008-03-22 21:12           ` Florian Weimer
2008-03-09  8:20   ` Robert Dewar's great article about the Strengths of Ada over other langauges in multiprocessing! Pascal Obry
2008-03-09  9:39     ` Georg Bauhaus
2008-03-09 12:40     ` Vadim Godunko
2008-03-09 13:37       ` Dmitry A. Kazakov
2008-03-09 14:41         ` Vadim Godunko
2008-03-10 20:51           ` Randy Brukardt
2008-03-10 22:30             ` Niklas Holsti
2008-03-10  9:56         ` Ole-Hjalmar Kristensen
2008-03-11 13:58       ` george.priv
2008-03-11 15:41         ` Vadim Godunko
2008-03-12  0:32           ` gpriv
2008-03-12 13:33             ` Maciej Sobczak
2008-03-12 14:41               ` gpriv
2008-03-12 15:22                 ` Vadim Godunko
2008-03-13  0:34                   ` gpriv
2008-03-12 16:28                 ` Maciej Sobczak [this message]
2008-03-12 17:24                   ` Samuel Tardieu
2008-03-13  8:41                     ` Maciej Sobczak
2008-03-13 15:20                       ` Samuel Tardieu
2008-03-12 23:54                   ` gpriv
2008-03-13  9:40                     ` Maciej Sobczak
2008-03-13 10:49                       ` Peter C. Chapin
2008-03-13 13:03                         ` Alex R. Mosteo
2008-03-13 14:02                           ` gpriv
2008-03-14  1:12                           ` Randy Brukardt
2008-03-14 10:16                             ` Alex R. Mosteo
2008-03-13 11:42                       ` gpriv
2008-03-13 16:10                         ` Maciej Sobczak
2008-03-13 16:16                           ` gpriv
2008-03-13 22:01                             ` Simon Wright
2008-03-13 22:25                             ` Maciej Sobczak
2008-03-14  2:07                               ` gpriv
2008-03-14  9:29                                 ` Maciej Sobczak
2008-03-14 21:54                                 ` Simon Wright
2008-03-15  2:29                                   ` gpriv
2008-03-15 13:29                                     ` Maciej Sobczak
2008-03-15 16:09                                       ` gpriv
2008-03-11 22:09       ` gpriv
2008-03-09 13:50     ` Maciej Sobczak
2008-03-09 14:54       ` Pascal Obry
2008-03-10 21:24   ` Randy Brukardt
2008-03-11 10:12     ` Alex R. Mosteo
2008-03-22 22:43     ` Florian Weimer
2008-03-26 13:49       ` Ole-Hjalmar Kristensen
2008-03-26 21:27         ` Florian Weimer
2008-03-27  9:31           ` Ole-Hjalmar Kristensen
2008-03-27 23:10             ` Florian Weimer
2008-03-28  9:51               ` Ole-Hjalmar Kristensen
2008-03-28 18:12                 ` Florian Weimer
2008-03-28 21:45                   ` Randy Brukardt
2008-03-31  7:59                   ` Ole-Hjalmar Kristensen
2008-03-31 13:03                     ` (see below)
2008-03-31 14:17                       ` (see below)
2008-04-01  9:02                       ` Ole-Hjalmar Kristensen
2008-04-01 14:12                         ` (see below)
2008-04-02  7:22                           ` Ole-Hjalmar Kristensen
2008-04-02 14:59                             ` (see below)
2008-04-04  6:36                               ` Ole-Hjalmar Kristensen
2008-04-04 13:56                                 ` (see below)
2008-04-04 17:36                                   ` Georg Bauhaus
2008-04-04 17:40                                     ` (see below)
2008-04-15 12:05                               ` Ole-Hjalmar Kristensen
2008-04-17  4:46                                 ` Randy Brukardt
2008-03-28  6:34             ` Randy Brukardt
2008-04-29  7:15   ` Ivan Levashew
2008-05-01  2:03     ` Steve Whalen
2008-03-14 19:20 ` Mike Silva
2008-03-14 20:43   ` Ed Falis
2008-03-22 22:51 ` Florian Weimer
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox