Sharing in D

Walter Bright newshound1 at digitalmars.com
Thu Jul 31 23:18:27 PDT 2008


Sean Kelly wrote:
> Would it be possible to do
> something like this and have the compiler realize it's safe:
> 
>     class SharedObj
>     {
>         void go() {}
>     }
> 
>     shared Mutex m;
>     shared SharedObj o;
> 
>     static this()
>     {
>         m = new Mutex;
>         o = new SharedObj;
>     }
> 
>     void someFunc()
>     {
>         synchronized( m )
>         {
>             o.go();
>         }
>     }

I don't know, but I doubt it.

>> Cast is the red flag raising feature. The thing about it is that when
>> you review code, the cast is where you look for bugs. With Java and C++,
>> you have *no clue* where to look for bugs. It reduces the problem domain
>> from the entire program to a (hopefully) manageable small subset.
> 
> Java and C++ have cast too, though.

But that is useless for multithreading, because Java and C++ types have 
no notion of sharing.

> Which
> reminds me... weren't we going to get tighter conversion rules for concrete
> types?  I'd love to have the D compiler yell at me for these things like my
> C++ compiler does.

All in goood time, my pretty <rubs hands>


> Back on track... I suppose I'm just worried that my apps will end up with
> casts all over the place simply to allow me to operate normally, thus
> diluting the usefulness of this red flag.  But that's idle speculation
> so perhaps my concern is unfounded.

I don't know how it will work out yet in practice, either. But I suspect 
if casts wind up all over the place, that may be a sign that there are 
too many connection points between the shared and unshared worlds, and 
perhaps a code refactoring is in order.



More information about the Digitalmars-d mailing list