H
hipek99
look at this:
#define _REENTRANT
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
struct _str *array[10000];
struct _str
{
int a;
char b[1000];
};
void t1()
{
int i=0;
for(i=0;i<10000;i++)
{
array = (struct _str*)malloc(sizeof(struct _str));
}
printf("malloc\n");
pthread_exit(NULL);
}
void t2()
{
int i = 0;
sleep(10);
for(i=0;i<10000;i++)
free(array);
printf("free\n");
pthread_exit(NULL);
}
main()
{
pthread_attr_t attr;
pthread_t t;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
pthread_create(&t,&attr,(void *)t1,NULL);
pthread_create(&t,&attr,(void *)t2,NULL);
sleep(20000);
}
why can't I free memory in t2 allocated in t1
if I remove pthread_exit(NULL) in t1 memory would be free
in a nutshell: I allocate global memory in one thread, then end this
thread, then free global memory in second thread, and when I look at
'ps aux' I notice that memory wasn't free
thanks for advice
hipek99
#define _REENTRANT
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
struct _str *array[10000];
struct _str
{
int a;
char b[1000];
};
void t1()
{
int i=0;
for(i=0;i<10000;i++)
{
array = (struct _str*)malloc(sizeof(struct _str));
}
printf("malloc\n");
pthread_exit(NULL);
}
void t2()
{
int i = 0;
sleep(10);
for(i=0;i<10000;i++)
free(array);
printf("free\n");
pthread_exit(NULL);
}
main()
{
pthread_attr_t attr;
pthread_t t;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
pthread_create(&t,&attr,(void *)t1,NULL);
pthread_create(&t,&attr,(void *)t2,NULL);
sleep(20000);
}
why can't I free memory in t2 allocated in t1
if I remove pthread_exit(NULL) in t1 memory would be free
in a nutshell: I allocate global memory in one thread, then end this
thread, then free global memory in second thread, and when I look at
'ps aux' I notice that memory wasn't free
thanks for advice
hipek99