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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:51:53+00:00 2026-05-19T04:51:53+00:00

I have a situation in C as well as in C++ which can be

  • 0

I have a situation in C as well as in C++ which can be best solved with something like the Python like decorators: I have few a functions which I’d like to wrap around with something else so that before the function enters some statements are performs and when it leaves some other functionality is executed.

For instance, I have a few functions in a library C file which when called should lock a semaphore and before returning the control to callee, should release the semaphore. without the lock they have following structure:

int f1(int)
{
    ...
    ... 
}

int f2(char*)
{
    ....
}

int f3(blabla)
{
    ....
}

... fn(...)

I’d like to define a global semaphore which should be locked before each of these functions is called and released when the function is returned. I’d like to do it with as much simplicity as possible; something close to this:

#lockprotected
int f1(int)
{
   ... /* nothing changed over here */
}
#endlockprotected

or something like

int f1(int)
{
   ... /* nothing changed over here */
}
#lockprotected f1

What I don’t want is:

  1. Change the function names as they are library functions and are being called from many places.
  2. Explicitly place any statement before return calls as most of the functions have many early returns in between. Or for that matter change any internals of the function.

What would be the most elegant way?

  • 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-05-19T04:51:54+00:00Added an answer on May 19, 2026 at 4:51 am

    Use RAII (resource acquisition is initialization) to define the lock on the mutex. This would allow you to forget about point #2, i.e., you don’t need to keep track of return statement to release the lock.

    class Lock {
    public:
        Lock () { // acquire the semaphore }
        ~Lock () { // release the semaphore }
    }
    

    Next create objects of this class at the start of your functions, i.e.,

    int f1 (int) {
        Lock l;
        // you can now forget about release of this lock
        // as ~Lock() will take care of it
    }
    

    A side advantage of this is that even in the case of exceptions being thrown from f1(), you still don’t need to worry about releasing the lock. All stack objects are destroyed before a function exits.

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

Sidebar

Related Questions

Situation Well, as you can see below, I have a main App, creating a
i have situation like this: class IData { virtual void get() = 0; virtual
I have situation in which I read a record from a database. And if
I have situation where a user can manipulate a large set of data (presented
I have situation like this: user submits form with action='/pay' in '/pay' I have
Well my situation is like this: I am generating a report as a text
I am wondering, which is the best and quickest way to get the well
I have a situation here: I have a page which is using to show
I have a development vm which is running sql server as well as some
I have situation, where running a query that filters by an indexed column in

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.