Solved

handling pthread_mutex_lock, pthread_mutex_unlock errors

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

828 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