synchronized { }

cemiller chris at dprogramming.com
Thu Jun 26 17:17:23 PDT 2008


On Thu, 26 Jun 2008 16:42:32 -0700, BCS <ao at pathlink.com> wrote:

> Reply to cemiller,
>
>> I use it on occasion. It can be useful for singleton:
>>  if(!foo)
>> {
>> synchronized
>> {
>> if(!foo)
>> foo = new Foo();
>> }
>> }
>> return foo;
>> especially useful because you don't need an object instance.
>>
>
> Hmmm. Slight variation on my last suggestion, Allow synchronized to take  
> any global symbol as the mutex
>
> if(!foo)
> {
>   synchronized(Foo)  // global mutex on Foo
>   {
>     if(!foo)
>       foo = new Foo();
>   }
> }
>
> Or if you need more resolution:
>
> class Foo
> {
>   alias void single;
> }
>
> ...
>   synchronized(Foo.single)  // global mutex on Foo.single
> ...


I could synchronized(typeid(Foo)) but I don't think D guarantees that each  
type has its own instance and monitor. This could potentially be a  
substitute; create a type and lock on it, no runtime allocation, not many  
language changes needed.



More information about the Digitalmars-d mailing list