Link to home
Start Free TrialLog in
Avatar of ambuli
ambuliFlag for United States of America

asked on

handling pthread_mutex_lock, pthread_mutex_unlock errors

Hi Experts,

Assuming I have the following code for creating a singleton, how can I handle if error is returned from pthread functions?  What is a good way to handle it.

Manager * Manager::getInstance()
{
    if (_instance == 0)
    {
        pthread_mutex_lock(&_instanceMutex);
        if (_instance == 0) // double checked
        {
            _instance = new Manager();
        }
        pthread_mutex_unlock(&_instanceMutex);
    }
    return _instance;
}

Open in new window


If I have a setter function like below, what could be the better way to handle pthread errors.

void DataHolder::setSomething(int data)
{
    pthread_mutex_lock(&m_dataMutex);
    m_data = data;
    pthread_mutex_unlock(&m_dataMutex);
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial