Find the answer to your Linux question:
Results 1 to 4 of 4
I have just started the pthread programming. The following program illustrates the sample program i am using. The problem I am facing in this program is that the fourth argument ...
  1. #1
    Just Joined!
    Join Date
    May 2007
    Posts
    8

    pthreads stack problem

    I have just started the pthread programming. The following program illustrates the sample program i am using. The problem I am facing in this program is that the fourth argument of the pthread_create sends the thread number which i am trying to print in the common thread function, but i am getting some bizzare values in the output. I am unable to explain this output as there is sufficient documentation to suggest the threads have their own stack. The output is shown at the end of the program. I am using linux on x86.

    #include <pthread.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <linux/unistd.h>

    _syscall0(pid_t, gettid);
    void *my_thread_func(void *);

    int main()
    {
    pthread_t thread[3];
    int i;

    for(i = 0; i < 3; i++)
    {
    if(pthread_create(&thread[i], NULL, my_thread_func, &i))
    {
    printf("Thread creation failed\n");
    goto err;
    }
    else
    printf("Thread successfully created\n");
    }

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

    err:
    perror("RESULT");
    }


    void *my_thread_func(void *value)
    {
    printf("Thread is running: %d threadID: %d\n", *(int *)value, gettid());


    }

    Output:
    ----------

    Sample 1
    -------------
    Thread successfully created
    Thread successfully created
    Thread successfully created
    Thread is running: 0 threadID: 2156
    Thread is running: 0 threadID: 2157 /* it is expected to print 1 instead of 0 */
    Thread is running: 2 threadID: 2158
    RESULT: Success

    Sample 2
    ------------
    Thread successfully created
    Thread successfully created
    Thread is running: 2 threadID: 2193/* it is expected to print 0 instead of 2 */
    Thread is running: 2 threadID: 2194/* it is expected to print 1 instead of 2 */
    Thread is running: 2 threadID: 2195
    Thread successfully created
    RESULT: Success

  2. #2
    Just Joined! djap's Avatar
    Join Date
    Jul 2005
    Location
    Not so sure anymore...
    Posts
    97
    I'm not much of an expert on this but I would say that the "void *value" you get in your thread function points to the adress of the i in the main thread and the i is changed later in main thread.

    Threads can access the variables in other threads using pointers and so I would say this behaviour is quite normal. Try for example create a list in your main thread, copy the values of i there and send the adress of the list's index instead of i so the value in the adress won't get changed later and see what happens. Something like
    Code:
    int values[3];
    .
    .
    .
    for(i = 0; i < 3; i++)
    {
     values[i] = i;
     if(pthread_create(&thread[i], NULL, my_thread_func, &values[i]))
    {
    .
    .
    .

  3. #3
    Just Joined!
    Join Date
    May 2007
    Posts
    8
    That's solved my problem. Thanks a lot for a prompt and quick reply.

    Can you also please answer the following query:
    documentation says :"...each created thread has it's own stack space"
    How it becomes possible for the created thread to access the stack space of the main() thread?
    In the above post you wrote "Threads can access the variables in other threads using pointers"........Can you also explain the mechanism, how is it done?

  4. #4
    Just Joined! djap's Avatar
    Join Date
    Jul 2005
    Location
    Not so sure anymore...
    Posts
    97
    Quote Originally Posted by div123 View Post
    That's solved my problem. Thanks a lot for a prompt and quick reply.

    Can you also please answer the following query:
    documentation says :"...each created thread has it's own stack space"
    How it becomes possible for the created thread to access the stack space of the main() thread?
    In the above post you wrote "Threads can access the variables in other threads using pointers"........Can you also explain the mechanism, how is it done?
    As I said I'm not an expert on this and what I'm about to write may wery well be wrong but I think different threads within same process operate in same memory adress space unlike different processes so as long as you have the adress of variable in different thread accessing the value is simple.

    In short different threads have their own stacks but they are in same adress space and visible to each other.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...