Solved

handling pthread_mutex_lock, pthread_mutex_unlock errors

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

910 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

19 Experts available now in Live!

Get 1:1 Help Now