shared/unshared classes

Jonathan M Davis jmdavisProg at gmx.com
Sat Feb 19 18:55:53 PST 2011


On Saturday 19 February 2011 18:26:25 Steven Schveighoffer wrote:
> I was working on an I/O library that I plan to use in development, and
> possibly submit to phobos, and I thought of this case.
> 
> A standard file can be shared or unshared.  In C, since there is no notion
> of shared/unshared, everything is shared.  So any writes/reads from a FILE
> * lock the object.
> 
> But in D, we can avoid those locks when the file is unshared.  However,
> this means I have to write two pretty much identical functions for each
> call.
> 
> Is there an expected way to do this?  I've really avoided doing anything
> with shared or threads since the new concurrency model came out, but with
> I/O, I'll have to deal with it.
> 
> I think a logical thing to do would be to have the shared version of the
> function call the unshared version after locking the object.  Is that a
> good idea?  Is the correct way to do this to mark the shared function
> synchronized, and then cast 'this' to unshared to call the other
> function?  Does this automatically happen with shared functions?

I would point out that per TDPL, either an entire class is synchronized or none 
of it is. You don't synchronize individual functions. Now, I don' think that 
that's the way that it's implemented at the moment, but that's the eventual 
situation as I understand it. So, your class shouldn't have a mixture of 
synchronized or unsynchronized. According to TDPL, it's illegal.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list