Sharing in D

downs default_357-line at yahoo.de
Fri Aug 1 07:25:33 PDT 2008


Walter Bright wrote:
> downs wrote:
>> I am looking for a safe way to transfer ownership of a single object
>> between threads.
>>
>> Like, for instance, a "unique Object foo" would behave like a shared
>> Object, but the compiler would treat it like a nonshared object for
>> purposes of optimization, and all accesses from the outside would be
>> implicitly synchronized.
>>
>> This could be used for objects that really are only supposed to exist
>> once, but still used in a multithreaded way.
>>
>> Hope that clears things up.
> 
> Sorry, I still have no idea what you're asking for.

Okay.

In the current system, every object "belongs" to every thread equally - synchronization blocks are used to prevent interference.

In the new system, this behavior, if I understand correctly, is equivalent to "shared".

The default, non-shared, would be objects that belong, permanently, to exactly one thread - the one that created them, to the extent that accessing them from another thread would constitute a compiler failure.

What I am looking for, then, is to somehow temporarily, or permanently, change the thread that a specific instance belongs to, transfer ownership to another thread, so to speak, in an atomic manner - so that at no time more than one thread possesses ownership of the object - thus gaining, basically, the guaranteed thread-safety of a non-shared object with the convenience of a shared object.

So it's basically a form of forced "synchronized" blocks - a thread that would try to access such an "unique" object, would first have to acquire ownership of it, to prevent threading issues - an action similar to the current "synchronized(this)". In fact, every access to an unique object would come down to an implicit "synchronized(obj)", which would allow us to treat the object as if it _were_ thread-local for the purpose of that access, because even though it isn't, no other thread could interfere with it.

Hope that clears things up :/



More information about the Digitalmars-d mailing list