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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:33:51+00:00 2026-06-05T08:33:51+00:00

Hello for above code in thread it is displaying 0 (tid = 0) instead

  • 0

Hello for above code in thread it is displaying 0 (tid = 0) instead of 8… what may be the reason ? In PrintHello function I am printing threadid but I am sending value 8 but it is printing 0 as output

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


void *PrintHello(void *threadid)
{
   int *tid;
   tid = threadid;
   printf("Hello World! It's me, thread #%d!\n", *tid);
   pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
   pthread_t thread1,thread2;
   int rc;
   int value = 8;
   int *t;
   t = &value;

   printf("In main: creating thread 1");
    rc = pthread_create(&thread1, NULL, PrintHello, (void *)t);
     if (rc)
    {
        printf("ERROR; return code from pthread_create() is %d\n", rc);
        exit(-1);
        }


   printf("In main: creating thread 2\n");
    rc = pthread_create(&thread1, NULL, PrintHello, (void *)t);
     if (rc)
    {
        printf("ERROR; return code from pthread_create() is %d\n", rc);
        exit(-1);
        }


   /* Last thing that main() should do */
   pthread_exit(NULL);
}
  • 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-05T08:33:54+00:00Added an answer on June 5, 2026 at 8:33 am

    The actual object that holds 8 is value which is local to your main function so accessing after main has exited is not valid.

    You don’t wait for your child threads to finish before they attempt to access this local variable so the behaviour is undefined.

    One fix would be to make your main wait for it’s child threads before exiting using pthread_join.

    (I’ve assumed that you’ve made a typo in your second call to pthread_create and meant to pass thread2 instead of thread1.)

    E.g.

    /* in main, before exiting */
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using jQuery 1.4.1 . a = $('#hello') .animate({height: '100px'}); a.hide(); Above code
for(var div_count=0;div_count<10;div_count++) { $(#div_+div_count).append(<div style='margin-left:'+incr_count+px;>Hello<div>); incr_count=incr_count+ 80 ; } The above Code is not
<div id='one' class='foo'>Hello</div> $('#one').replaceWith(<div id='one'>World</div>) Above code is producing <div id='one' class='foo'>World</div> However the
$('a').click(function(event){ $('body').html('loading...'); $.post('www.sitename.com/hello',{site:http//:www.google.com},function(data) { alert(data); }); event.preventDefault(); }); I am using the above script
Hello I have the following code namespace ConsoleApplication2 { class Program { static void
Hello! I'm not even sure if this is possible, but hopefully it is in
Hello everyone I'm currently having 2 issues with the code below: Upon return of
Let's say I have the following HTML code <div id="outer"> <div id="inner">Hello World</div> </div>
function test() { this.str = hello; this.sayHello = function() { document.write(this.str); } this.init =
I have below code in html <div id=divTest> Hello <input type=text id=txtID/> <input type=text

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.