A monitor for every object

Robert Jacques sandford at jhu.edu
Sat Feb 5 07:36:23 PST 2011


On Sat, 05 Feb 2011 03:00:31 -0500, Jérôme M. Berger <jeberger at free.fr>  
wrote:

> Robert Jacques wrote:
>> On Fri, 04 Feb 2011 17:23:35 -0500, Jérôme M. Berger <jeberger at free.fr>
>> wrote:
>>
>>> Steven Schveighoffer wrote:
>>>> D's monitors are lazily created, so there should be no issue with
>>>> resource allocation.
>>>     What happens if two threads attempt to create a monitor for the
>>> same object at the same time? Is there a global lock to avoid race
>>> conditions in this case?
>>>
>>>         Jerome
>>
>> Only the reference to the mutex is shared, so all you need in an atomic  
>> op.
>
> 	This requires an atomic "if (a is null) a = b;". I did not know
> that such a beast existed.
>
> 		Jerome

Yes, the beast exists and it's the basis for most lock-free programming as  
well as lock implementations. It's generally known as compare and swap or  
CAS (see http://en.wikipedia.org/wiki/Compare_and_swap and  
core.atomic.cas). There's also another atomic primitive called  
Load-Link/Store-Conditional, which is available an several non-x86  
architectures (ARM, PowerPC, etc) and is generally considered a more  
powerful primitive than CAS.


More information about the Digitalmars-d mailing list