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 7924903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:00:47+00:00 2026-06-03T18:00:47+00:00

https://stackoverflow.com/a/5524120/462608 If you want to call functions recursively, which lock the same mutex, then

  • 0

https://stackoverflow.com/a/5524120/462608

If you want to call functions recursively, which lock the same mutex, then they either
have to use one recursive mutex, or
have to unlock and lock the same non-recursive mutex again and again (beware of concurrent threads!), or
have to somehow annotate which mutexes they already locked (simulating recursive ownership/mutexes).

Can in any case this be a “sensible” design decision to make function recursive which already has a mutex lock?

  • 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-03T18:00:53+00:00Added an answer on June 3, 2026 at 6:00 pm

    In addition to paxdiablo’s exmaple using an actual recursive funciotn, don’t forget that using a mutex recursively doesn’t necessarily mean that the functions involved are recursive. I’ve found use for recursive mutexes for dealing with a situation where you have complex operations which need to be atomic with respect to some data structure, with those complex operations rely on more fundamental operations that still need to use the mutex since the fundamental operations can be used on their own as well. An example might be something like the following (note that the code is illustrative only – it doesn’t use proper error handling or transactional techniques that might really be necessary when dealing with accounts and logs):

    struct account
    {
        mutex mux;
    
        int balance;
    
        // other important stuff...
    
        FILE* transaction_log;
    };
    
    void write_timestamp( FILE*);
    
    
    // "fundamental" operation to write to transaction log
    void log_info( struct account* account, char* logmsg)
    {
        mutex_acquire( &account->mux);
    
        write_timestamp( account->transaction_log);
        fputs( logmsg, account->transaction_log);
    
        mutex_release( &account->mux);
    }
    
    
    // "composed" operation that uses the fundamental operation.
    //  This relies on the mutex being recursive
    void update_balance( struct account* account, int amount)
    {
        mutex_acquire( &account->mux);
    
        int new_balance = account->balance + amount;
    
        char msg[MAX_MSG_LEN];
        snprintf( msg, sizeof(msg), "update_balance: %d, %d, %d", account->balance, amount, new_balance);
    
        // the following call will acquire the mutex recursively
        log_info( account, msg);
    
        account->balance = new_balance;
    
        mutex_release( &account->mux);
    }
    

    To do something more or less equivalent without recursive mutexes means that the code would need to take care not to reacquire the mutex if it already held it. One option is to add some sort of flag (or thread ID) to the data structure to indicate if the mutex is already held. In this case, you’re essentially implementing recursive mutexes – a trickier bit of work than it might seem at first to get right. An alternative is to pass a flag indicating you already acquired the mutex to functions as a parameter (easier to implement and get right) or simply have even more fundamental operations that assume the mutex is already acquired and call those from the higher level functions that take on the responsibility of acquiring the mutex:

    // "fundamental" operation to write to transaction log
    //  this version assumes that the lock is already held
    static
    void log_info_nolock( struct account* account, char* log msg)
    {
        write_timestamp( account->transaction_log);
        fputs( logmsg, account->transaction_log);
    }
    
    
    // "public" version of the log_info() function that
    //      acquires the  mutex
    void log_info( struct account* account, char* logmsg)
    {
        mutex_acquire( &account->mux);
        log_info_nolock( account, logmsg);    
        mutex_release( &account->mux);
    }
    
    
    // "composed operation that uses the fundamental operation
    //      since this function acquires the mutex, it much call the
    //      "nolock" version of the log_info() function
    void update_balance( int amount)
    {
        mutex_acquire( &account->mux);
    
        int new_balance = account->balance + amount;
    
        char msg[MAX_MSG_LEN];
        snprintf( msg, sizeof(msg), "update_balance: %d, %d, %d", account->balance, amount, new_balance);
    
        // the following call assumes the lock is already acquired
        log_info_nolock( account, msg);
    
        account->balance = new_balance;
    
        mutex_release( &account->mux);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From here: https://stackoverflow.com/a/5524120/462608 If you want to lock several mutex-protected objects from a set
I just finished looking at this question: https://stackoverflow.com/questions/753122/which-cloud-computing-platform-should-i-choose But, I am not certain what
Example form, https://stackoverflow.com/questions/ask and I save this page to desktop. Then I edit the
Possible duplication (solved): https://stackoverflow.com/a/1133132/783469 I have icons (jpg, png) for my application, which is
As per https://stackoverflow.com/questions/3264227/relations-with-multiple-keys-in-doctrine-1-2 , I have two tables which (as I can't get it
This is the same question as this: https://stackoverflow.com/questions/2890620/jquery-running-a-function-in-a-context-and-adding-to-a-variable But that question was poorly posed.
I have virtually the same question as https://stackoverflow.com/questions/3300504/finding-out-all-websites-hosted-by-a-webhosting-service ... I'm trying to get shared
I try to call the code from this link https://stackoverflow.com/a/9100406/942113 with gwt jsni. My
In https://stackoverflow.com/a/130741/168646 I learned about closetag.vim, which is exactly what I was hoping to
Same question as https://stackoverflow.com/questions/3100878/ipad-frame-programmatically-doesnt-match-with-ib but no-one answered! I created a UITableCell layout in IB,

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.