pthread_attr_setdetachstate(...) has a problem

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.

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;
}
The output is :
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top