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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:52:54+00:00 2026-05-25T06:52:54+00:00

Could you please show me the way how I can print the parent first

  • 0

Could you please show me the way how I can print the parent first using a mutex…Thanks

void *print_message_function( void *ptr );

main() {

     pthread_t thread1;
     char *message1 = "Thread 1";
     int  iret1;


     iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);


     pthread_join( thread1, NULL);

     printf("(Parent)Thread 1 returns: %d\n",iret1);
     exit(0);
}

void *print_message_function( void *ptr ) {

     char *message;
     message = (char *) ptr;
     printf("(Child)%s \n", message);
}
  • 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-25T06:52:54+00:00Added an answer on May 25, 2026 at 6:52 am

    By acquiring a lock before creating a thread and then immediately trying to acquire that lock in your new thread you can guarantee that some code after the thread is created will execute before the thread can possibly have acquired the lock. Of course it would be simpler just to run the code before creating the thread in the first place.

    #include <stdio.h>
    #include <pthread.h>
    #include <stdlib.h>
    
    void *print_message_function( void *ptr );
    
    static pthread_mutex_t mut;
    
    int main() {
    
         pthread_t thread1;
         char *message1 = "Thread 1";
         int  iret1;
         pthread_mutex_init(&mut, NULL);
         pthread_mutex_lock(&mut);
         /* lock before creating the thread */    
         iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
    
    
         printf("Parent first\n");
         /* realasing the lock gives the child a chance to acquire it */
         pthread_mutex_unlock(&mut);
    
         pthread_join( thread1, NULL);
    
         /* NOTE: iret1 is the result of the create call, not what the thread itself returned */
         printf("(Parent)Thread 1 returns: %d\n",iret1);
         return 0;
    }
    
    void *print_message_function( void *ptr ) {
         /* parent already has this lock, so this will always block until they release it */
         pthread_mutex_lock(&mut);
         char *message;
         message = (char *) ptr;
         printf("(Child)%s \n", message);
         pthread_mutex_unlock(&mut);
    
         return NULL;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the example below, can someone please show me how this could be called?
Could some one please show me how I can rewrite the below method in
Is there a way I could show what the hind-side camera captures on a
I have been trying out some LINQ query can someone please show how to
I can't find any good resource for parsing with regular expression. Could someone please
Wondering if someone could please explain the difference between these two queries and advise
I am missing something in my SQL query, could please someone advise how to
I'm a little lost in general, but if someone could please briefly explain why
Could someone please tell me why this <%= destroy_password_url @user.password_reset_token %> generates http://localhost:3000/api/destroy_password.4G5EoRVYMUAtiIKqOerKsw routes.rb
Could someone please suggest why this is happening... I’ve got some code to pretty

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.