[cpp-threads] High-level concurrency issues

Peter Dimov pdimov at mmltd.net
Tue Oct 18 13:09:27 BST 2005


Herb Sutter wrote:

> For example, imagine someone writes an object that lazily initializes
> itself from a file or database, and when the lazy initialization gets
> triggered it acquires a file or database lock to perform its
> initialization. Now say you put an object of that type into a
> synchronized container like a .NET SyncHashTable (a monitor), and the
> latter in particular will lock(this) and then call object.GetHashCode
> -- which can trigger a lazy read and acquire a lock. That's fine in
> itself, but of course now you have a deadlock just waiting to happen...

What alternative model can avoid this issue? I eliminate lock-free from 
consideration because it's very difficult to write correct lock-free code. 
An asynchronous hash table, where all requests are put on a queue and the 
client code receives notifications when an operation completes and returns a 
result? Doesn't this increase the complexity of the client code by quite a 
factor? 




More information about the cpp-threads mailing list