Boost logo

Boost :

From: vicente.botet (vicente.botet_at_[hidden])
Date: 2008-06-18 10:33:28


Hi,

the recursive_mutex constructor initialize/set/destroy the
pthread_mutexattr_t every time.
I'm wondering if we can improve the efficiency storing this on a static
variable which will be initialized once.
What could be the better way to initialize this static variable?

Note: the same applies to interprocess mutexes.

recursive_mutex()
{
// begin code that can be initialized only once
    pthread_mutexattr_t attr;
    int const init_attr_res=pthread_mutexattr_init(&attr);
    if(init_attr_res)
    {
        throw thread_resource_error();
    }
    int const
set_attr_res=pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
    if(set_attr_res)
    {
        throw thread_resource_error();
    }
// end code that can be initialized only once
    int const res=pthread_mutex_init(&m,&attr);
    if(res)
    {
        throw thread_resource_error();
    }
// begin code that can be removed
    BOOST_VERIFY(!pthread_mutexattr_destroy(&attr));
// end code that can be removed
}

Regards,

Vicente


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