Thread local and memory allocation

Walter Bright newshound2 at digitalmars.com
Tue Oct 4 11:30:33 PDT 2011


On 10/4/2011 2:32 AM, deadalnix wrote:
> Le 04/10/2011 10:52, Walter Bright a écrit :
>>
>> Allocate an object, then cast it to immutable, and pass it to another
>> thread.
>
> That is explicitly said to be unsafe on D's website. As long as a reference
> exist in the creating thread, this should work, but if thoses references
> disapears, you'll end up with memory corruption.

Unsafe doesn't mean "undefined behavior". It just means the compiler cannot 
guarantee that you did it correctly. If you do it correctly, it still should work.

On the other hand, "undefined behavior" cannot be done correctly.

With casts to immutable, it is perfectly correct if you, the user, ensure that 
there are no other mutable references to the same data. It's just that the 
compiler itself cannot make this guarantee, hence it's "unsafe".

Casting from immutable to mutable, on the other hand, is "undefined behavior" 
because neither the compiler nor you, the user, can guarantee it will work.


More information about the Digitalmars-d mailing list