Shared Hell

#ponce aliloko at gmail.com
Wed Oct 28 02:37:28 PDT 2009


Denis Koroskin Wrote:

> I've recently updated to DMD2.035 (from DMD2.031 because all the later  
> versions had issues with imports) and for the first time faced problems  
> with shared modifier.
> 
> I don't need shared and all my globals are __gshared (they are globally  
> unique instances that don't need per-thread copies).
> 
> Yet some of methods of the class hierarchy (a root singleton class and  
> everything which is accessible through it) are synchronized (well, you  
> know why). That's where the problems begin.
> 
> Marking a method as synchronized automatically makes it shared (more or  
> less obvious). And marking the method shared makes it unable to invoke  
> with non-shared instance (and __gshared != shared), meaning that I'm  
> unable to use my __gshared variables anymore, making this attribute  
> useless for any serious safe programming.
> 
> So I started with replacing __gshared with shared and quickly understood  
> how viral it is. Not only you mast mark all the members shared (methods  
> and field), instantiate classes with shared attribute, you also have to  
> create a duplicate all the methods to make them accessible with both  
> shared and non-shared (thread-local) instances:
> 
> class Array(T)
> {
>      const(T) opIndex(uint index) const
>      {
>          return data[index];
>      }
> 
>      T opIndex(uint index)
>      {
>          return data[index];
>      }
> 
>      const(T) opIndex(uint index) shared const
>      {
>          return data[index];
>      }
> 
>      shared(T) opIndex(uint index) shared
>      {
>          return data[index];
>      }
> 
>      private T[] data;
> }
> 
> And that's just opIndex. Ooops...
> 
> But not only that, every interface now have to specify the same method  
> twice, too:
> 
> interface Target
> {
>      bool build();
>      bool build() shared;
>      void clean();
>      void clean() shared;
>      bool ready();
>      bool ready() shared;
>      void setBuildListener(BuildListener buildListener);
>      void setBuildListener(shared BuildListener buildListener) shared;
> }
> 
> That's a bit frustrating. Most importantly, I don't even need shared  
> (__gshared was more than enough for me), yet I'm imposed on using it.
> 
> Oh, I can't use any of the druntime/Phobos classes (e.g. create an  
> instance of shared(Thread)), because none of them are shared-aware.
> 
> I think the design needs to be given a second thought before the plane  
> takes off because I'm afraid it may painfully crash.

Wow.
I certainly won't switch to D2 if every getter must be written 4 times (and 4 times more with immutable(T) ?).

I don't even understand the fundamental difference between __gshared 
and shared : is this only transitivity ?





More information about the Digitalmars-d mailing list