Boost logo

Boost :

From: Don G (dongryphon_at_[hidden])
Date: 2005-02-13 14:00:29

Hi All,

For my own work in the thread-safe smart pointer arena, I have used a
technique to reduce the number of mutex objects from one per object,
yet minimize induced contention. The basic idea is a static pool
of mutexes indexed by hashed pointers. Or in pseudo code:

    mutex & get_object_mutex (const void * p) // see below
        const size_t pool_size = 127;

        static mutex pool[mutex_pool_size];

        unsigned long ul = reinterpret_cast<unsigned long>(p);

        unsigned long hash = ...; // munge ul in some clever way

        return pool[ hash % pool_size ];

    class foo
        void bar ()
          #ifdef BOOST_HAS_THREADS
            mutex::scoped_lock guard(get_object_mutex(this));

            // ... do something ...

This is a tad simplified. For example, I used my Singleton template
manage the pool, but the idea is the same. Instead of allocating one
mutex per object that might need one, allocate a pool of them and
the object's pointer so that all references to the same object use
same mutex.

Has anyone considered using this technique in shared_count? The extra
contention could be controlled by the sizeof the pool, and it would
a big savings where lots of objects are used. It would also be better
than the lightweight_mutex used on some platforms (such as Windows)
achieve similar savings. At least that would be so if the mutex used
where a CRITICAL_SECTION and not a kernel mutex.



Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around

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