- Joined
- Sep 8, 2006
- Messages
- 3
- Reaction score
- 0
Hi All,
I have a problem with the pthread_attr_setdetachstate(......) function.
I am making this thread non joinable, but after creating the thread.
What I understand is that the state of a thread cannot be changed after creating it once.
So I am creating the thread first and then trying to change its state.
So this should have no effect on the thread. However, from the output, we can figure out that the created thread did join with the main thread and then on fetching the state of the thred using pthread_attr_getdetachstate(...), it sends the state to be 1 which is non joinable. So did the state change after creation and joining and before exiting?
Please comment.
The output is :
Why is this thread joining even though the state is 1 which means non joinable?
I have a problem with the pthread_attr_setdetachstate(......) function.
I am making this thread non joinable, but after creating the thread.
What I understand is that the state of a thread cannot be changed after creating it once.
So I am creating the thread first and then trying to change its state.
So this should have no effect on the thread. However, from the output, we can figure out that the created thread did join with the main thread and then on fetching the state of the thred using pthread_attr_getdetachstate(...), it sends the state to be 1 which is non joinable. So did the state change after creation and joining and before exiting?
Please comment.
Code:
#include<pthread.h>
#include<stdio.h>
void *prn1(void *u)
{
int state;
printf("In prn1\n");
if(pthread_attr_getdetachstate((pthread_attr_t*)u, &state))
printf("detach error\n");
printf("state is %d\n",state);
pthread_exit(NULL);
}
int main()
{
pthread_t t1;
pthread_attr_t attr1;
int state1 = 1;
if(pthread_attr_init(&attr1))
printf("pthread_attr_init ERROR!!\n");
if(pthread_create(&t1, &attr1, prn1, &attr1))
printf("pthread_create ERROR!!\n");
if(pthread_attr_setdetachstate(&attr1, state1))
printf("pthread_attr_getdetachstate ERROR!!\n");
printf("state1 is %d\n", state1);
pthread_join(t1, NULL);
return 0;
}
Code:
shubh@Vintcerf thread]$ gcc -pthread thread5.c
[shubh@Vintcerf thread]$ ./a.out
state1 is 1
In prn1
state is 1
[shubh@Vintcerf thread]$
Why is this thread joining even though the state is 1 which means non joinable?