synchronized { }
Steven Schveighoffer
schveiguy at yahoo.com
Fri Jun 27 07:23:28 PDT 2008
"cemiller" wrote
> On Thu, 26 Jun 2008 16:42:32 -0700, BCS 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.
I think this is guaranteed as long as you are not loading D from multiple
DLLs.
In any case, Tango avoids needing this statement by making mutexes fully
accessible objects. If you need a mutex without having it attached to an
object, it's easy:
Mutex m = new Mutex;
synchronized(m)
{
}
-Steve
More information about the Digitalmars-d
mailing list