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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:49:41+00:00 2026-05-28T17:49:41+00:00

can you explain me why the following use of pthread_join doesn’t work?it blocks my

  • 0

can you explain me why the following use of pthread_join doesn’t work?it blocks my code.
if I comment those 3 lines, my code does what is expected, but obviously I don’t know if threads are terminated(in my code there is no problem for that, but in a bigger situation there is).

int k=0;
pthread_mutex_t mutex= PTHREAD_MUTEX_INITIALIZER;

struct primi{
   int s;
   int temp;
};
struct example{
   int c;
   struct primi primissimi;
};
void *funzione_thread(void* args);

void prepare(void){
   printf("preparing locks...\n");
   pthread_mutex_lock(&mutex);   }
void parent(void){
   printf("parent unlocking locks...\n");
   pthread_mutex_unlock(&mutex);}
void child(void){
   printf("child unlocking locks...\n");
   pthread_mutex_unlock(&mutex);  }
void *thr_fn(void *arg){
   printf("thread started...\n");
   return(0);}


void *funzione_thread(void* args){
   pthread_mutex_lock(&mutex);
   struct example *exthread = args;
   struct example locale = *exthread;
   locale.primissimi.s++;pthread_mutex_unlock(&mutex);
   printf("local is %d original is %d\n",locale.primissimi.s,exthread->primissimi.s);
   exthread->primissimi.s = locale.primissimi.s;
   printf("after it is%d\n",exthread->primissimi.s);
   pthread_exit(NULL);
}


int ffork(struct example *extmp){
  pthread_t id[5];
  int i;
  while(k<3){
    k++;
    pthread_create(&id[k],NULL,funzione_thread,extmp);
    }
  printf("now k is %d\n\n",k);
  for(i=1;i<=3;i++){
    pthread_join( id[i] ,NULL );
    printf("waited thread %d\n",i);
    }
  printf("threads completed\n");
  pthread_exit (NULL);  
  //return 1;
}
 int main(int argc, char** argv){

  struct example *ex = malloc(sizeof(*ex));
  int pid,tmp;
      pthread_t tid;

      if ((err = pthread_atfork(prepare, parent, child)) != 0){
           printf("can't install fork handlers");
           exit(-1);}
      pthread_create(&tid, NULL, thr_fn, 0);
      sleep(1);
      pid=fork();
      if(pid==0){
         ex->c=1;
         ex->primissimi.s=1;
         if((tmp=ffork(ex))!=1){
        printf("errore in ffork\n");
        sleep(2);
        exit(0);
        }
    else{printf("tutto ok in ffork\n");
        sleep(2);
        exit(0);
        }
    }//fine figlio

else{
    sleep(10);
}
return 0;
 }
  • 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-28T17:49:41+00:00Added an answer on May 28, 2026 at 5:49 pm

    Your code has no protection against calling fork with the mutex locked. Since the thread that locked the mutex doesn’t exist in the child, it cannot unlock the mutex … ever. Any thread in the child that tries to acquire the mutex will deadlock, waiting for the non-existent thread to release the lock.

    There are a lot of possible fixes, but the simplest is probably to hold the mutex across the call to fork. You can use an atfork handler to do this. Arrange the handler to acquire the mutex before the fork and release it after (in both the parent and child).

    You really need to know what you’re doing to use fork together with pthreads, unless you’re going to immediately exec in the child.

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

Sidebar

Related Questions

Can anyone explain what the following PHP Code does function query($query_string) { if ($query_string
Can someone explain why the following code... #!/opt/local/bin/perl use strict; use warnings; my $string;
Can someone please explain why the following doesn't work works: function displayResults(data) { $(#odNextPage).click(function()
Can anyone explain why following code won't compile? At least on g++ 4.2.4. And
Can anyone explain the following PHP Code ? function get_param($param_name, $param_type = 0) {
Can someone explain the following code snippet for me? // Bind base object so
Can somebody explain why the following code is not valid? Is it because the
Can someone explain what the following does? private HashSet nodes[]; nodes = new HashSet[21];
can somebody explain why is the following trivial code (implementation of Euclid's algorithm to
Can anyone explain the following behaviour to a relative newbie... const char cInputFilenameAndPath[] =

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.