Atomic updates

monarch_dodra monarchdodra at gmail.com
Tue Jan 22 10:45:36 PST 2013


On Tuesday, 22 January 2013 at 18:10:27 UTC, cal wrote:
> On Tuesday, 22 January 2013 at 09:47:25 UTC, monarch_dodra 
> wrote:
>> Avoids deadlock.
>>
>> imagine 2 threads:
>> a: from 2 to 5.
>> b: from 5 to 2.
>>
>> If both threads acquire their first lock, then you have a dead 
>> lock, and your program is basically dead.
>>
>> By always locking low first, you avoid the deadlock in a 
>> rather simple but elegant way.
>
> Ah neat. And what about the case from = to? Why doesn' that 
> deadlock in this code? (Concurrency is rather new to me)

Because a single thread may acquire the same lock more than once. 
At which point, it will increment the lock counter. The lock is 
released when the counter reaches zero.

This allows having easy "nested" logic, where a function does not 
have to worry if the caller function already has a lock.


More information about the Digitalmars-d-learn mailing list