Solved

handling pthread_mutex_lock, pthread_mutex_unlock errors

Posted on 2012-03-30
1
543 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

758 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now