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: Thu, 13 Mar 2008 02:40:47 -0700 (PDT)
Date: 2008-03-13T02:40:47-07:00	[thread overview]
Message-ID: <2c2989ba-a109-40d6-b0a3-f91d94a2d291@a1g2000hsb.googlegroups.com> (raw)
In-Reply-To: 44104211-afd5-4cf7-8467-90471d4afd1b@f63g2000hsf.googlegroups.com

On 13 Mar, 00:54, gp...@axonx.com wrote:

> You are replying on posts without reading them carefully.  The issue
> is covered in the thread below:
>
> http://www.codeguru.com/forum/archive/index.php/t-442321.html

Unfortunately, most of the participants have no idea about the subject
(they even admit it), so their discussion is not a good authority.
The only exception there is Paul McKenzie, who actually claimed
clearly that *volatile has nothing to do with threads*.

There is a link to Microsoft documentation with example. This can be
treated as an authoritative source of information related to Microsoft
compilers *only* (and I have doubts even about this). Since the C++
standard does not define the semantics of volatile in the presence of
multiple threads, then any guarantee given by Microsoft should be
treated only as *language extension*. In particular, the same code
example will not work with other compiler.

To be more exact, if Microsoft claims that their example works with
their compiler, it means that references to volatile provide the
release-acquire memory consistency, which in turn means that they
involve memory barries. This is surprising, but let's check the
assembly output from their compiler...

OK, I've checked it with Visual Studio 2005 (I don't have access to
anything newer).
The generated code does *not* involve memory barriers. It will not
work.

A simpler example can explain the problem.
Consider the following two variables:

bool A = false;  // can be as well volatile
bool B = false;  // can be as well volatile

and two threads:

// Thread1:
A = true;
B = true;

// Thread2:
if (B)
{
    assert(A);
}

*From the point of view* of Thread1, only the following states are
visible, as times goes on downwards:

time 1:   A == false && B == false
time 2:   A == true  && B == false
time 3:   A == true  && B == true

In other words, from the point of view of Thread1, the following
situation:

A == false && B == true

can *never* happen, because the order of writes just makes it
impossible. In other words, the condition and assertion from Thread2
would be correct if executed in Thread1.

The problem is that Thread2 can see something different and there the
assertion can fail. This can happen for two reasons:

1. If you write something to memory, you only instruct the CPU that
you *want* something to be stored. It will do it - but not necessarily
immediately and it can actually happen *some time later*.
The problem is that if you store two values (separate store
instructions), the CPU does not know that they are related - and can
stored them physically in different order. The compiler has no control
over it! It is the hardware that can reorder the writes to memory.

2. If you read something from memory, you can actually read something
that is already in the cache - the value can be there already and
*earlier*.

Both these mechanisms can make Thread2 see different order of
modifications than that perceived by Thread1.
Again, the compiler has no control over it. It is all in hardware.

This is also a reason for why the Double-Check Locking Pattern does
not work. Please read this:

http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html

It describes the problem for Java, but mentions the same ordering
issues.

Don't use volatile for multithreading. It does not work.

Note: Ada gives guarantees for proper ordering for volatile objects.

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



  reply	other threads:[~2008-03-13  9:40 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
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 [this message]
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