From: Boehm, Hans on
On Jul 2, 2:14 pm, Louis Semprini <lsempr...(a)hotmail.com> wrote:
> Hello,
>
> I have read ISO/IEC JTC1 SC22 WG21 N2668 (a revision of N2647, which
> is a revision of N2534).
>
> "Concurrency Modifications to Basic String"http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2668.htm
>
> N2668 corrects a few typos from the previous versions, but there is
> still one paragraph left which has me stumped:
>
> Can anyone explain the following claim from the paper:
>
> |Thus the only real anomaly seems to be that creating the initial
> |non-const reference invalidates previously generated const
> |references. This anomaly has unfortunate consequences.
> |Typically, when v offers "container thread safety", we can do
> |
> | #pragma omp parallel for
> | for( size_t i = 0, n = v.size(); i < n; ++i ) {
> | v[i] = 0;
> | }
> |
> |However, for a basic_string v, we must insert v[0];
> |before the pragma to render the code correct.
>

Rereading this, I'm inclined to agree that this example was misplaced
and doesn't really demonstrate the preceding point. I don't see what
this has to do with const references either. Sorry about that.

IIRC, the issue demonstrated by the example is the following. Assume
that v.size() is 2, and the two loop iterations are run on separate
threads. Thread 0 and thread 1 end up computing the references to the
respective v[i] concurrently. If v was previously in a shared state,
they may end up independently deciding to unshare and step on each
other. Based on a quick look at the g++ implementation, it does have
this issue.

This is possibly fixable with a sufficiently expensive CoW
implementation. But the natural extension of the C++03 spec to
threads probably treats unsharing operations as modifications of the
container, so the generation of the two references would indeed race,
and this shouldn't be expected to work.

Inserting the v[0] in front ensures that the string is not shared
before the parallel loop begins. Hence no unsharing will take place,
and there will be no race.

Hans


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 | 
Pages: 1
Prev: Perl-like qx// in C++ ?
Next: Current FCD lambdas