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
    {
     public:
        void bar ()
        {
          #ifdef BOOST_HAS_THREADS
            mutex::scoped_lock guard(get_object_mutex(this));
          #endif

            // ... do something ...
        }
    };

This is a tad simplified. For example, I used my Singleton template
to
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
hash
the object's pointer so that all references to the same object use
the
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
be
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)
to
achieve similar savings. At least that would be so if the mutex used
where a CRITICAL_SECTION and not a kernel mutex.

Best,
Don

=====

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk