Something needs to happen with shared, and soon.

Walter Bright newshound2 at digitalmars.com
Wed Nov 14 03:00:35 PST 2012


On 11/14/2012 2:49 AM, Benjamin Thaut wrote:
> Am 14.11.2012 11:42, schrieb Walter Bright:
>> On 11/14/2012 1:23 AM, Benjamin Thaut wrote:
>>> Could you please give an example where it would break?
>>
>> Thread 1:
>>    1. create shared object
>>    2. pass reference to that object to Thread 2
>>    3. destroy object
>>
>> Thread 2:
>>    1. manipulate that object
>>
>
> But for passing a reference to a value type you would have to use a pointer,
> correct? And pointers are a unsafe feature anyway...
> I don't see your point.

Pointers are safe. It's pointer arithmetic that is not (and escaping pointers).


> And if the use of pointers is allowed, I can make the same case break in a
> single threaded environment without shared.

1. You can't escape pointers in safe code (well, it's a bug if you do).

2. If the struct is on the heap, it is only destructed if there are no 
references to it in any thread. If it is not on the heap, and you are in safe 
code, it should always be destructed safely when it goes out of scope.

This is not so for shared pointers.


More information about the Digitalmars-d mailing list