Reputation: 1671
I am approaching to pthreads in C and I'm starting writing very dumb programs to get the hang of them. I tried to create a program containing two threads, they should print their name and their status should be collected as they terminate their execution. So my code is:
//Function declaration
void *state_your_name(char*);
//Function definition
//Passing a string as parameter for
//outputting the name of the thread
void *state_your_name(char *name) {
//Declaration of the variable containing the return status
void* status;
//Printing out the string
printf("%s\n", name);
//Exiting the thread and saving the return value
pthread_exit(status);
}
int main(void) {
pthread_t tid_1, tid_2;
void * status;
//Creating thread 1...
if (pthread_create(&tid_1, NULL, state_your_name, "Thread 1")) {
printf("Error creating thread 1");
exit(1);
}
//Creating thread 2...
if (pthread_create(&tid_2, NULL, state_your_name, "Thread 2")) {
printf("Error creating thread 2");
exit(1);
}
//Waiting for thread 1 to terminate and
//collecting the return value...
if (pthread_join(tid_1, (void *) &status)) {
printf("Error joining thread 1");
exit(1);
}
printf("Thread 1 - Return value: %d\n", (int)status );
//Waiting for thread 2 to terminate and
//collecting the return value...
if (pthread_join(tid_2, (void *) &status)) {
printf("Error joining thread 2");
exit(1);
}
printf("Thread 2 - Return value: %d\n", (int)status );
return 0;
}
I would expect an output like this:
Thread 1
Thread 2
Thread 1 - Return value: 0
Thread 2 - Return value: 0
but my issue is the return value of Thread 1
is 733029576
, but Thread 2
returns 0
as predicted; it's like the status variable is uninitialized and containing garbage. What am I missing?
Upvotes: 3
Views: 717
Reputation: 3908
You are returning a pointer to status
that is defined in the thread's stack. That memory may go away after the thread exits. You should not declare status
inside your thread. You are better off just doing return 0
inside state_your_name()
.
See How to return a value from thread in C
Upvotes: 2
Reputation: 1429
To get a predictably different return value, try this
void *state_your_name(char *name) {
//Declaration of the variable containing the return status
void* status= (void *)(int)name[0];
....
This will return the ascii code of the first letter of the name.
Also, the implementations of pthread_join I know, take a void** as last parameter, so
if (pthread_join(tid_2, &status)) {
would be correct (your casting it (void*)
being sort of misleading)
Upvotes: 0
Reputation: 726579
The reason you see a garbage value in the output is that the local void *status
variable of state_your_name
is uninitialized:
void *state_your_name(char *name) {
//Declaration of the variable containing the return status
void* status = NULL; // <<===== Add initialization here
//Printing out the string
printf("%s\n", name);
//Exiting the thread and saving the return value
pthread_exit(status);
}
With this change in place, your program should produce the output that you expect.
Note that returning status
directly from state_your_name
is an alternative to calling pthread_exit
: you can replace that call with return status
.
Upvotes: 5
Reputation: 753785
Your code in state_your_name()
does not initialize the status
that it returns, so you get indeterminate information back. You're unlucky that you're getting zero rather than garbage.
Upvotes: 2