Issue 8077 - Regression (2.058): .reserve does not work for shared arrays
Summary: Regression (2.058): .reserve does not work for shared arrays
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: druntime (show other issues)
Version: D2
Hardware: All All
: P2 regression
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-05-10 02:39 UTC by Andrej Mitrovic
Modified: 2012-05-16 11:30 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2012-05-10 02:39:07 UTC
This worked in 2.057:

void main()
{
    shared int[] a;
    a.reserve(1024);
}

Now:
D:\DMD\dmd2\windows\bin\..\..\src\druntime\import\object.di(597): Error: template object.reserve(T) cannot deduce template function from argument types !()(shared(int[]),int)
Comment 1 Walter Bright 2012-05-16 11:06:45 UTC
I'm going to argue that although the code compiled in 2.057, it was not correct and had latent concurrency bugs. The problems are:

1. being shared doesn't mean that assignments to an array are atomic

2. there is no obvious place to put a lock around updating a shared resource like a shared array

3. locks can be very, very slow. Shared does not mean "automatically insert locks"

Therefore, I'm going to mark this as invalid.

In essence, you (the programmer) has to decide how to manage the memory is to be controlled by a shared array. You can allocate an array as thread local, then cast it to shared as a last step (probably the best approach). If the array is already shared, you'll have to protect the resize with a lock that fits in with your data structure.
Comment 2 Andrej Mitrovic 2012-05-16 11:30:15 UTC
Well I thought shared was going to become some sort of gateway to safe-by-default concurrent programming, I guessed wrong.

This section needs to be updated (by someone who knows exactly what shared does):
http://dlang.org/attribute.html#shared

Some partial information is here:
http://dlang.org/migrate-to-shared.html#shared