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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:24:35+00:00 2026-05-26T00:24:35+00:00

Sorry if a question title is confusing. I just wanted to put all the

  • 0

Sorry if a question title is confusing. I just wanted to put all the things together.
I have a piece of code like:

int newThread(int(*pfunc)())
{
   pthread_t tid;
   pthread_create(&tid, NULL, pfunc, NULL);
   int i = 0;
   while(threads[i] != 0 && i < MAX_NUM_THREADS)
   {
      if ((MAX_NUM_THREADS - 1) == i)
      {
        puts("We've run out of threads' number limit\n");
        return 1;
      }
      ++i;
   } 
   threads[i] = tid;
   pthread_join(tid, NULL);
   return 0;
}

threads[] is a global array. I want to make this function reentrant, but this means I shouldn’t use global variables as far as I understand. I guess that’s because values of global variables can be unpredictable at a certain time. But in my case the array seems to be quite predictable.

  1. Is it ok, if I lock array with a mutex to make this function reentrant?
  2. If yes, then how do I do it right? Just lock the first element before using it and unlock after? or is it better to lock/unlock every element while accessing it?
  3. Is this even possible to make this function reentrant?
  • 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-26T00:24:35+00:00Added an answer on May 26, 2026 at 12:24 am

    To say a function is reentrant, it should only rely on local variables to be simultaneously called by two (or more threads) and return correct results.

    If the function rely on some shared data, (we can’t really made it reentrant), we can make it thread-safe to be called simultaneously by two (or more) threads if all access to the shared data is serialized.

    To make your function thread-safe, you should lock the loop and the insertion into threads[]. If you lock only the loop part, somebody could modify the content of threads between the end of the loop and the affectation at rank i.

    pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
    
    int newThread(int(*pfunc)())
    {
       pthread_t tid;
       pthread_create(&tid, NULL, pfunc, NULL);
       int i = 0;
       pthread_mutex_lock(&mymutex);          // take the lock
       while(threads[i] != 0 && i < MAX_NUM_THREADS)
       {
          if ((MAX_NUM_THREADS - 1) == i)
          {
            puts("We've run out of threads' number limit\n");
            pthread_mutex_unlock(&mymutex);   // don't forget to release the lock here too :)
            return 1;
          }
          ++i;
       } 
       threads[i] = tid;
       pthread_mutex_unlock(&mymutex);        // release the lock
       pthread_join(tid, NULL);
       return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello everybody and sorry for the rather confusing question-title. I have a Hibernate Class
Sorry about the vague question title, I just want to ascertain some things. Static
First of all Sorry if my question title sounds stupid.... I have the following
Sorry about the rubbish question title. I have a table SET_DEFINITIONS like this: SETKEY
Sorry if the question title is confusing. Let me explain further. I am building
Sorry about the extremely vague question title (any suggestions for improvements welcome) I have
Sorry about the wording for my question title. I have a basic HTML anchor
Hello and sorry for the confusing title of my question. My app assigns a
Sorry for the bad question title. Let's say that I have DateRange class that
Sorry for the confusing title of the question. I am uncertain about how should

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.