I am trying to declare a variable pointer of data type int, to store the thread id
(i.e. myid) and then set that equal to explicit type cast to integer pointer of parameter vargp. But, when I try to output an ID number, I get an address as opposed to an actual value. The for loop is supposed to run for LOOP iterations. Here's what I have so far for the previous statement. int *myid = (int *)vargp;
Here is the rest of the function.
void* threadFunction(void *vargp) {
int LOOP = 0;
unsigned long i = 0;
int *myid = (int*) vargp;
printf("\n-----------------------------\n");
printf("\nThread %d has started\n", *myid);
printf("\n-----------------------------\n");
while (LOOP <= 5) {
printf("\nThread ID %d is printing iteration %d", *myid, LOOP);
LOOP ;
}
printf("\nThread %d has finished\n", *myid);
return NULL;
}
I get Thread ID -136702208 is printing iteration 0
as supposed to Thread ID 1 is printing iteration 0
How can I fix this?
Here is the function that calls the threadfunction.
void multiThreads() {
int SIZE = 5;
int i = 0;
int error;
pthread_t tid[SIZE];
for (i = 0; i < SIZE; i ) {
error = pthread_create(&(tid[i]), NULL, threadFunction, (void*) &(tid[i]));
if (error != 0) {
printf("\nThread can't be created : [%s]", strerror(error));
}
} //end for
while (i < SIZE) {
pthread_join(tid[i], NULL);
i ;
}
}
CodePudding user response:
As the content of a pthread_t
is undefined, I changed tid
to a struct
that holds both the pthread_t t
and the numeric id
value you wanted. Initialize the id and pass the address of id
in as as arg. Changed SIZE to size as uppercase is usually for values you define. Using main()
instead of multiThreads()
to drive the unchanged threadFunction()
:
int main() {
int size = 5;
struct {
pthread_t t;
int id;
} threads[size];
for(unsigned i=0; i < size; i ){
threads[i].id = i 1;
int error = pthread_create(&threads[i].t, NULL, threadFunction, &threads[i].id);
if(error) {
printf("\nThread can't be created : [%s]", strerror(error));
}
}
for(unsigned i = 0; i < size; i ) {
pthread_join(threads[i].t, NULL);
}
return 0;
}
Here the snippet of the output:
-----------------------------
Thread 1 has started
-----------------------------
Thread ID 1 is printing iteration 0
Thread ID 1 is printing iteration 1
Thread ID 1 is printing iteration 2
Thread ID 1 is printing iteration 3
Thread ID 1 is printing iteration 4
Thread ID 1 is printing iteration 5
Thread 1 has finished
-----------------------------
Thread 5 has started
-----------------------------
Thread ID 5 is printing iteration 0
Thread ID 5 is printing iteration 1
Thread ID 5 is printing iteration 2
Thread ID 5 is printing iteration 3
Thread ID 5 is printing iteration 4
Thread ID 5 is printing iteration 5
Thread 5 has finished
-----------------------------
Thread 3 has started
-----------------------------
Thread 4 has started
-----------------------------
Thread ID 4 is printing iteration 0
Thread ID 4 is printing iteration 1
Thread ID 4 is printing iteration 2
Thread ID 4 is printing iteration 3