i have problem with this thread...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mhrt2008
    New Member
    • May 2009
    • 6

    i have problem with this thread...

    hi all
    i am using thread to print the value of x i though the value will be 10, but i found it 0????? Why , and haw can i adjust this thread to print x=10

    Code:
    #include <stdio.h>
    #include <pthread.h>
    
    
    void *thread1(void *param);
    int x;
    
    int main(int argc, char *argv[])
    {
    
    
    pthread_t tid[1]; 
    pthread_attr_t attr;  
    
    pthread_attr_init(&attr);
    
    pthread_create(&tid[0],NULL,&thread1, NULL);
    
    pthread_join(tid[0],NULL);
    
      printf("x = %d\n",  x); 
    }
    void *thread1(void *param)
    { 
    
               int x=10;
                    
         pthread_exit(0);
    }

    i am waiting
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    In the thread you declare x in the scope of the thread hiding the global declared one which is initialised to 0 and never has its value changed.

    Comment

    • mhrt2008
      New Member
      • May 2009
      • 6

      #3
      OK...any idea can help me to print x=10
      i am thinking to do swap with another variable

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by mhrt2008
        OK...any idea can help me to print x=10
        i am thinking to do swap with another variable
        The answer was already given to you but here it is again in other words: remove line #26.

        kind regards,

        Jos

        Comment

        Working...