Solved

handling pthread_mutex_lock, pthread_mutex_unlock errors

Posted on 2012-03-30
1
556 Views
Last Modified: 2012-03-31
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

0
Comment
Question by:ambuli
1 Comment
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 37790112
>> What is a good way to handle it.

Drastically reduce the chance errors might be returned by :

1) ensuring that the mutex is properly initialized before trying to use it.
2) being careful with recursive locking (your current code doesn't show that, unless the Manager constructor calls Manager::getInstance, or the same mutex is used outside of Manager::getInstance, for different purposes eg. -- don't do either of this).
3) ensuring there are matching unlock's for every lock, and an unlock is not performed if the current thread doesn't own the lock.
4) being careful with thread priorities (don't have them too high) together with PTHREAD_PRIO_PROTECT.

If all these conditions are true, then an error can really only occur if something is seriously wrong, in which case, you can do something drastic like halt the application, or ...


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

Same rules as above. Being extra careful about recursively locking the mutex.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
linux redhat 7.2 10 74
AWS Central Authentication 1 66
awk variable in printf 1 25
Why  my code (program) build with old compiler? 11 47
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question