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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:49:49+00:00 2026-06-18T09:49:49+00:00

I have a function ( my_func ) which can be called by a program

  • 0

I have a function (my_func) which can be called by a program that has multiple thread. And I don’t want this function to be executed twice or more in the same time. Because I’m writing in memory, and I don’t want them to write in the same time.

void my_func()
{
    // I want a line that blocks the execution if one is still pending
    /* the code here */
}
  • 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-18T09:49:50+00:00Added an answer on June 18, 2026 at 9:49 am

    use mutex lock at the beginning of your function and mutex unlock before going out from your function

    pthread_mutex_t my_func_mutex = PTHREAD_MUTEX_INITIALIZER;
    
     void my_func()
        {
            pthread_mutex_lock(&my_func_mutex); // at the beginning of your function
            .....
            pthread_mutex_unlock(&my_func_mutex); // do not forget to unlock your mutex before going out from your function
        }
    

    You can do it in other way if you want:

    pthread_mutex_t my_func_mutex = PTHREAD_MUTEX_INITIALIZER;
    #define MY_FUNC_MACRO() \
        do { \
            pthread_mutex_lock(&my_func_mutex); \
            my_func(); \
            pthread_mutex_unlock(&my_func_mutex); \
        } while(0)
    
     void my_func()
        {
            .....
        }
    

    and in your code you call MY_FUNC_MACRO() instead of my_func()

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

Sidebar

Related Questions

In python I have a function which has many parameters. I want to fit
I have this function on my controller (Im using CodeIgniter) that reads the database,
I have an embedded Python program which runs in a thread in C. When
I have a function that takes another function as a parameter. Something like this
Sometimes in my code I have a function which can take an argument in
My problem is this, i have a function which is stored in an array
Suppose I have this function: void my_test() { A a1 = A_factory_func(); A a2(A_factory_func());
I have a function - myFunc() in class A. There are multiple other classes
Imagine I have a template function like this: template<typename Iterator> void myfunc(Iterator a, typename
Suppose I have a function: function my_function($a, $b, $c) { ... } I want

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.