[dmd-concurrency] tail-shared by default?

Michel Fortin michel.fortin at michelf.com
Sat Jan 9 20:14:54 PST 2010


Le 2010-01-09 à 13:35, Sean Kelly a écrit :

>> I disagree. If you have a private class member which is shared it might be because the class itself will give its address to another thread.
>> 
>> Take this example:
>> 
>> 	class ObjectGenerator {
>> 		private shared Object latestObject_;
>> 
>> 		this() {
>> 			startThreadToUpdateObjectAtRegularIntervals(&latestObject_);
>> 		}
>> 
>> 		shared Object latestObject() @property {
>> 			return sharedRead(latestObject_);
>> 		}
>> 	}
> 
> How often have you passed the address of a reference anywhere?  I can't think of a single time I've done this. In the spare few instances where this happens, it seems totally reasonable that use of the reference should be atomic.

I think this is a very useful example. Make the object immutable and look at it as a simplistic but very efficient message passing system between threads for when you only need to remember the latest message. (The message might express the current state of something and you never need to remember the past states.)

We'd need to make the object immutable for this to work well though:

	class ObjectGenerator {
		private shared(Rebindable!(immutable Object)) latestState_;

		this() {
			otherThreadPleaseUpdateWhenStateChanges(&latestObject_);
		}

		immutable(Object) latestState() @property {
			return sharedRead(latestState_);
		}
	}

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/





More information about the dmd-concurrency mailing list