Boost logo

Boost :

Subject: [boost] Making a new contribution - weak_hash_map
From: Yoni Lavi (l_yoni_at_[hidden])
Date: 2008-11-27 20:09:33

I'd like to contribute a C++ class template that implements something similar to java.util.WeakHashMap; it uses boost::intrusive::iunordered_set and boost::pool::fast_pool_allocator internally.

This is the first time I'm looking to get into boost development in general.

I'm not aware of any trivial way to implement this using existing components of the boost library, but I could have easily overlooked something so let me know if I am mistaken. I'd also like to know if there's any interest in such a contribution at all - anyone else thinks this class could be useful?

Briefly, given a key space and value space, the class provides a way to lookup (and optionally create) values by a unique key, in a thread safe manner. A smart pointer to the entry (<key, value> pair) is returned from the lookup/create function. The tricky part is to make such entries automatically destroy themselves when no longer in use. The set itself cannot store a shared_ptr or the use count would never reach zero; it has to rely on weak_ptr and try to lock() it.

There is also a race condition that needs to be taken care of (lock() may fail if refcount just dropped to zero in another thread). Lastly, it also has to handle the case where the weak_hash_map is destroyed but its entries persist, and again this needs to be properly synchronized.

The implementation makes use of insert_check and insert_commit to implement a "create-or-get" function as well. I have also written an intrusive smart pointer type and that made it possible to use raw pointers instead of the weak_ptr's in the implementation.

It's not that this stuff is rocket science as much as it involves lots of boilerplate code (e.g. making an entry hasher out of a key hasher, key/entry and entry/entry equality from key/key equality, resizing the hash table automatically, synchronization, allocation... the class I've written takes care of a lot of the ugly details.

If anyone's interested, please fill me in on the process of making such a contribution? Thanks.


Boost list run by bdawes at, gregod at, cpdaniel at, john at