Boost logo

Boost Users :

Subject: [Boost-users] Shared mutex vs normal mutex
From: Alessandro Bellina (abellina_at_[hidden])
Date: 2009-08-25 08:27:55


Hello
I am testing the boost mutex/lock classes in order to implement a
multiple reader single writer model.

Because of this I thought that the shared mutex with lock_shared and
lock_unique would be perfect. I'm finding that the performance is no
better than with a simple mutex.

My writer is adding a bunch of elements, locking the shared container
exclusivelly until it is done. The readers looks at all elements of
the container, locking on a per element basis.

Any ideas as to why the shared mutex would be slower in this case than
the simple mutex?

This is the code for my producer thread and my consumers (as a side
note, employee_set is a multi index):

typedef boost::shared_mutex mutex_type;
typedef boost::shared_lock<mutex_type> read_lock_type;
typedef boost::unique_lock<mutex_type> write_lock_type;

static mutex_type rw_mutex;
                
                
struct ConsumerThread{
        ConsumerThread (employee_set* e):es(e){}
                                        
        void operator()(){
                
                for (int i=0; i<es->size(); i++){
                        read_lock_type l(rw_mutex);
                        es->get<1>();
                }
                
        }
                                        
        
        employee_set* es;
                        
        
};

struct ProducerThread{
        
        ProducerThread(int n, employee_set* e):N(n),es(e){}
                                        
        void operator()(){
                
                write_lock_type l(rw_mutex);
                for (int i=0; i<N; i++){
                        es->insert (employee(i, "TestEmployee",100-i));
                }
                l.unlock();
                
                
        }
        
        int N;
        employee_set* es;
                
};



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net