Thread: do threads have the same variable in start routine? pthread

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    33

    do threads have the same variable in start routine? pthread

    we know that, we need to specify a start routine when we call pthread_create()
    imagine that:
    Code:
    void *start_routine () {
        int i;
        for (i=0; i<10; i++) {
            printf ("%d\t", i);
        }
        printf ("\n");
    }
    if we have 5 threads, do they have the same variable "i" in start routine(they share "i"), or do they have their own copy of "i"?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    It's a local variable, so it's local to each thread.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    33
    but they got the same place, they execute same code.
    they made a copy of variables in start routine, when they entry the routine?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    Each thread has a separate stack.
    Local variables follow stack scoping rules.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-04-2011, 10:07 AM
  2. How make static variable for across threads?
    By 6tr6tr in forum C++ Programming
    Replies: 10
    Last Post: 04-22-2008, 08:32 AM
  3. a point of threads to create multiple threads
    By v3dant in forum C Programming
    Replies: 3
    Last Post: 10-06-2004, 09:48 AM
  4. pthread_create start routine without args
    By discostu in forum C Programming
    Replies: 0
    Last Post: 04-09-2003, 11:01 PM
  5. I need a variable display routine
    By frenchfry164 in forum C++ Programming
    Replies: 2
    Last Post: 12-05-2001, 08:09 PM