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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:59:32+00:00 2026-06-16T23:59:32+00:00

i have this piece of code: #include <stdlib.h> #include <stdio.h> #include <pthread.h> #include <time.h>

  • 0

i have this piece of code:

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>

int number;
pthread_mutex_t  *mutex;
pthread_t *threads;

void *PrintHello(void *threadid)
{
    long tid;
    tid = (long)threadid;
    printf("Hello World! It's me, thread #%ld!\n", tid);
    time_t rawtime;
    struct tm * time_start;
    time ( &rawtime );
    time_start = localtime ( &rawtime );
    printf ( "The number %ld thread is created at time %s  \n",tid, asctime     (time_start));
    pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
    int i,rc;

    printf("give threads");
    scanf("%d",&number);
    mutex = calloc( number, sizeof(*mutex));
    threads = calloc(number, sizeof(*threads));

    for (i = 0; i < number;i++) {
        pthread_mutex_init( &mutex[i],NULL);
    }

    for(i=0; i<number; i++){
        printf("In main: creating thread %d\n", i);
        rc = pthread_create(&threads[i], NULL, PrintHello, (void *)i);
        if (rc){
            printf("ERROR; return code from pthread_create() is %d\n", rc);
            exit(-1);
        }
    }

    return 0;
}

The purpose of this code is to ask the user to give the number of the threads that it is going to create and after the creation of the threads,the thread itself will print 2 messages saying the number of the thread and the time that it was created.

The problem is that inside the main the message "In main: creating thread" is showing up but the message inside the threads sometimes does not.

For example, it would create 3 threads and only 1 thread will show up its message and that thread also will not show up the time that was created. I dont know if there is something wrong with the code.

  • 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-16T23:59:33+00:00Added an answer on June 16, 2026 at 11:59 pm

    Root Cause:
    Your main() exits before the child threads get an opportunity to complete their task.

    Solution:
    You need to make sure that the main() waits until all the threads have completed their work.
    You need to use:

    pthread_join()

    to achieve this functionality. Add the following before returning from main():

    for(i=0; i<number; i++)
    {
        pthread_join(threads[i], NULL);
    }
    

    Other problems:

    • You allocate memory dynamically but never deallocate it. Eventhough, the OS reclaims the memory once your program returns. It is a good practice to do so explicitly.
    • You created a mutex but you neither use it, nor deallocate it. But that seems to be code in progress.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>
I have a piece of code shown below #include <stdio.h> #include <stdlib.h> void Advance_String(char
I have a problem with this piece of code in C. #include <stdio.h> #include
I have written this piece of code: #include <iostream> using namespace std; double function(int
Say I have this piece of code. #include <string> int main() { std::string(0); return
I have the following piece of code: #include <stdio.h> int main ( int argc,
I was playing with this code: #include <stdlib.h> #include <stdio.h> void function(int *p){ free(p);
I wrote this piece of code #include <stdio.h> /* Input/Output */ #include <stdlib.h> /*
I have this piece of code and i don't know how it works #include
I have this piece of code to count the number of posts in a

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.