DConf 2013 Day 1 Talk 2: Copy and Move Semantics in D by Ali Cehreli

Jonathan M Davis jmdavisProg at gmx.com
Thu May 16 12:52:35 PDT 2013


On Thursday, May 16, 2013 11:13:27 Regan Heath wrote:
> So, who's responsibility is it to ensure the function/method call executes
> without errors caused by mutable shared data?

I think that for the most part, the question of thread-safety and const that 
you've been discussing is moot. const by itself is thread-local by definition. 
Whether you use const or immutable really has no impact on thread safety - not 
from the perspective of the function being called anyway. The only way that 
you can end up having to worry about thread-safety in such functions is if the 
caller casts away shared on a variable and then passes it to the function. And 
by definition, at that point it's the responsibility of the one doing the cast 
to make sure that they don't break the type system (since they've circumvented 
the type system by casting away shared). immutable avoids even that issue, 
because it's implicitly shared but can be treated by code as being thread-
local (since it won't change), but the function accepting the string doesn't 
care. Worrying about that is completely up to the code that cast away shared.

Without shared, the one area where the function risks the data changing when 
it was passed as const is when that function has another, mutable handle to 
the same data, and the data is mutated via that handle. If the function is 
never mutated the same type as the const parameter (either directly or 
indirectly), then there's no chance that the const parameter will change.

So, while using immutable reduces the odds of threading issues, I think that 
it's quite clear that the function itself doesn't have to worry about that. 
And the type system won't even _let_ it worry about it, because the type 
system assumes that const without shared is thread-local, and without passing 
the appropriate mutex to the function, the function wouldn't be able to 
protect itself from mutation from another thread anyway (in the case where the 
const parameter was shared underneath the hood with shared cast away).

- Jonathan M Davis


More information about the Digitalmars-d-announce mailing list