Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9115965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:33:33+00:00 2026-06-17T04:33:33+00:00

Am quite sure this is a naive question but still going to ask it

  • 0

Am quite sure this is a naive question but still going to ask it nevertheless since I couldn’t find much help on net. I am writing a c++ code using pthreads . The following is an outline of one of the methods

void Method1()
{
    try
    {
    pthread_mutex_lock(&syncLock);
    statement1;
    statement2;
    pthread_mutex_unlock(&syncLock);
    statement3;
    statement4;
    }
    catch(exception& e)
    {
    log error
    }
}

where syncLock is of type pthread_mutex_t and is initialized using the default initialization.

My question is that if an exception is thrown in the statement2 of the method then the control passes directly to the catch statement. In that case my mutex remains locked. But I also cant simply write an unlock statement in the catch block since if the exception came from statement 3 then the mutex has been unlocked and calling unlock on an already unlocked mutex results in undefined behavior.

So is there a way I can check whether the mutex has been unlocked or not in the catch statement. Or, is there a C# style lock statement alternative available in c++ so I am sure that the lock is released even if an exception is thrown from within that block

I am not inclined to use libraries for this purpose. A simple custom solution will be much appreciated

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:33:35+00:00Added an answer on June 17, 2026 at 4:33 am

    Write a tiny RAII-style locker class:

    class lock { 
        pthread_mutex_t &mutex;
    public:   
        lock(pthread_mutex_t &m) :mutex(m) { pthread_mutex_lock(&mutex); }
        ~lock() { pthread_mutex_unlock(&mutex); }
    };
    

    Then write your code something like this:

    void Method1()
    {
        try
        {
            { 
                lock l(syncLock);
                statement1;
                statement2;
             }
             statement3;
             statement4;    
        }
        catch(exception& e)
        {
        log error
        }
    }
    

    When you exit from the scope where you defined the lock, the lock will be destroyed via the destructor, thus calling pthread_mutex_unlock. That happens whether you exit the scope normally or via an exception. When you enter the exception handler, you know the mutex has been unlocked.

    Note that C++11 has this built into the standard library as std::lock_guard (and has a number of mutex types to use with it).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is going to be quite a vague question but I am wondering how
Not quite sure how to word this question. I am wondering if there is
Wasn't quite sure how to word this but let's say I've used ssh to
I'm sure this has been asked before but I'm not quite sure how to
Im quite annoyed at having to ask this but I cant get it to
I'm quite sure this is a silly error but I'm unable to spot it.
I'm quite sure this is trivial, but for the life of me I can't
I'm quite sure this has been asked before but I can't for the life
I am not quite sure if this question is relevant here; maybe it's too
I'm not quite sure if this should be consider as a C++ question or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.