0

I wrote the following code to create N number of threads and print the thread ID of each thread.

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


void *threadFunction (void *);

int main (void)
{

   int n=0,i=0,retVal=0;
   pthread_t *thread;

   printf("Enter the number for threads you want to create between 1 to 100 \n");
   scanf("%d",&n);

   thread = (pthread_t *) malloc (n*sizeof(pthread_t));

   for (i=0;i<n;i++){
       retVal=pthread_create(&thread[i],NULL,threadFunction,(void *)&i);
       if(retVal!=0){
           printf("pthread_create failed in %d_th pass\n",i);
           exit(EXIT_FAILURE);        
       }
   }

   for(i=0;i<n;i++){
        retVal=pthread_join(thread[i],NULL);
            if(retVal!=0){
               printf("pthread_join failed in %d_th pass\n",i);
               exit(EXIT_FAILURE);        
            }
   }

}

void *threadFunction (void *arg)
{
    int threadNum = *((int*) arg);

    pid_t tid = syscall(SYS_gettid);

    printf("I am in thread no : %d with Thread ID : %d\n",threadNum,(int)tid);


}

the Argument i am passing to each thread is a counter i which increments from 0 to n-1 for each new thread. But while at the output i see i has the value zero for all the threads, not able to undestand , can somebody please explain.

  Enter the number for threads you want to create between 1 to 100 
  5
  I am in thread no : 0 with Thread ID : 11098
  I am in thread no : 0 with Thread ID : 11097
  I am in thread no : 0 with Thread ID : 11096
  I am in thread no : 0 with Thread ID : 11095
  I am in thread no : 0 with Thread ID : 11094
haris
  • 2,003
  • 4
  • 25
  • 24
  • 2
    You pass the same address of variable `i` to the thread worker. When the threads access the value referred to by the pointer, the main loop has already set `i = 0`. – Nominal Animal Jun 13 '17 at 10:05
  • Possible duplicate of [pthread\_create : passing an integer as the last argument](https://stackoverflow.com/questions/19232957/pthread-create-passing-an-integer-as-the-last-argument) – Stargateur Jun 13 '17 at 10:07

1 Answers1

1

The problem lies in the below line:

retVal=pthread_create(&thread[i],NULL,threadFunction,(void *)&i);

Don't pass the address of i, as i keeps changing in the main function. Instead pass the value of i and typecast it appropriately in the thread function and use.

For Example, pass the value like below:

retVal=pthread_create(&thread[i],NULL,threadFunction,(void *)i);

In the thread function access as below:

void *threadFunction (void *arg)
{
    int threadNum = (int)arg;

    pid_t tid = syscall(SYS_gettid);

    printf("I am in thread no : %d with Thread ID : %d\n",threadNum,(int)tid);


}
Jay
  • 24,173
  • 25
  • 93
  • 141
  • You'll want to make that `(void *)(intptr_t)i` and `= (int)(intptr_t)arg;`, respectively, in case `int` and `void *` are of different sizes. `intptr_t` is defined in `` (which is automatically included if one includes ``). – Nominal Animal Jun 13 '17 at 10:10