Reputation: 3316
What is the impact of freeing the struct that holds the pthread_t on the thread itself? I have a struct that represents a thread:
typedef struct car{
int cur_place;
pthread_t car_thread;
}car;
and i have an array that holds these cars, after some time i want to free the struct from inside the thread, i mean:
void * car_thread(void * number){
int num = *(int *)number;
free(maze[num]);
maze[num] = NULL;
pthread_exit(NULL);
}
is it possible? what will happen to the thread after i free the pthread that holds it? will the it run the next lines?
thanks in advance.
Upvotes: 2
Views: 100
Reputation: 393
The thread will not exit until pthread_exit() is called or, in the case you have signal handling built in, a signal for exiting has been received. The thread is attached to the process but different threads are isolated entities until you tie them up in any kind of thread organization. Apologize for the vague phrasing but this the best way I can describe it.
If you intend to signal the cars to exit when you free the data structure, you need to have signal handling build in to notify each thread to exit. Or, call pthread_exit() in each thread somehow.
Upvotes: 0
Reputation: 6116
You have just freed the location storing thread's ID, the data structure which stores thread attributes is freed when you do pthread_exit(NULL)
. Therefore answer to your question: thread stll exists.
Upvotes: 2
Reputation: 5083
Freeing car
only releases the memory used to store those values. The thread will still be other there somewhere possibly. Think of pthread_t
as simply holding a number or address used by the system to talk about the thread. Not the thread itself.
Just don't refer to the memory of car
anywhere after its free'd.
Upvotes: 3