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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:46:54+00:00 2026-06-17T22:46:54+00:00

I have this small program that I found in an exam subject of an

  • 0

I have this small program that I found in an exam subject of an OS course.

void * func (void * p) {
    int n = p;
    printf("%d \n",n);
    return NULL;
}

int main() {
    int i;
    pthread_t t[3];
    for(i=0; i<3; i+=1)
        pthread_create(&t[i] ,NULL, func, (void*)i);
    return 0;
}

When I run it, I get the following results (with a new line after each digit):

1st run : 0 0
2nd run : 1 0 2 2
3rd run : 0 1 1

Why does it print 4 digits when I only create 3 threads. And how can it print duplicates?

The code is compiled with gcc in Ubuntu.

screenshot of the terminal

  • 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-17T22:46:55+00:00Added an answer on June 17, 2026 at 10:46 pm

    You do not join your threads before exiting main(). Add the following into main():

    for(i=0; i<3; i+=1)
        pthread_join(t[i], NULL);
    

    Not joining the threads leads to undefined behavior when the threads continue to execute while the program is exiting. Undefined behavior is free to do anything, including printing duplicates.

    Think of it this way, the void* that is passed to the thread is stored somewhere, and once you prematurely exit main you could be destructing the data to pass to the thread, at which point it can take on any value (including duplicate ones). But this isn’t even worth trying to explain since it is undefined behavior.

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

Sidebar

Related Questions

I have this small class looking like this: private static int field1 = -
I have this small class called City that simply holds some information about a
I have this small PHP script that takes a POST variable and writes it
I have this small piece of code that basically takes a list and runs
I have written this small program in VS2010 to run on Outlook 2007. It
I found this: http://msdn.microsoft.com/en-us/library/vslangproj80.reference3%28VS.80%29.aspx what I have in mind is that many of the
I have a small java program that searches the contents of all *.txt files
I have a program i frequently use that is made with .NET. This program
I have a small program, that sends sms messages over SerialPort. It works nice
I have a C# program that is supposed to pull data from a small,

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.