Operating System - Linux
1754333 Members
3096 Online
108813 Solutions
New Discussion

free posix thread resources

 
SOLVED
Go to solution
Ignacio Javier
Regular Advisor

free posix thread resources


Hello:

I am tring to free thre resources generated by treads but i can´t.
The programa i have made is very simple. Ii generates many threads that wait a certain amount of time and then finish.
What i do not understand is why when they finish thre resources are not free.

Is it posible that the SO frees them later. ?
Do you think this is the normal behavior of the SO ? I have seen many threads tutorials,manual examples and i do not see anything diferent of what i have in the code.
Do you think my code is wrong ?

#include
#include

void *HiloPrueba(void *);
pthread_t hilo_escucha;
void *res_exit;

int main(int argc, char *argv[])
{
printf("\nMirar consumos");
fflush(stdout);
sleep(20);
for (int i=1;i<=100;i++)
{
pthread_create(&hilo_escucha, NULL, HiloPrueba,NULL);
fflush(stdout);
}
printf("Todos los hilos creados\n");
printf("30 segs para mirar consumo\n");
sleep(100);
fflush(stdout);
}



void *HiloPrueba(void *arg)
{
printf("Hilo creado\n");
pthread_detach(pthread_self());
sleep(30);
printf("\nSe acabo el tiempo");
fflush(stdout);
pthread_cleanup_push();
pthread_exit(NULL);
}


I am wathing the values of memory this process is using with glance plus.

Thanks in advance.
2 REPLIES 2
Dennis Handly
Acclaimed Contributor
Solution

Re: free posix thread resources

>I am trying to free the resources generated by threads but i can't.

The resources are freed in order for your process to use them again. Of course your kernel thread has been removed. But all of the memory has been put back into the pool only for that process.

>Is it possible that the SO frees them later?
>Do you think this is the normal behavior of the OS?

If by SO you mean OS, yes. Yes, normal.

>Do you think my code is wrong?

No, only your expectations. :-)

>I am watching the values of memory this process is using with glance plus.

Are you looking at memory segments?
Since each thread stack is mmapped, that can be freed. But libpthread maintains a pool of thread stacks for the next pthread_create. I wouldn't think it would have 100 but it may have all, since you created them all with the default stacksize.
Ignacio Javier
Regular Advisor

Re: free posix thread resources


Thanks.

I have asked HP and they told me the way is working is fine. It is doing what is supposed to.

Regards