shared/unshared classes

Steven Schveighoffer schveiguy at yahoo.com
Sat Feb 19 18:26:25 PST 2011


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?

Actually, this whole question is really general, because I'd expect to  
have to do this with any object that was shared -- putting memory barriers  
around accessing the object's members isn't enough to guarantee  
concurrency, and it makes no sense to do the memory barriers when the  
accesses are protected by a lock, right?

-Steve


More information about the Digitalmars-d-learn mailing list