/*http://hankjin.blog.163.com/blog/static/33731937201072675024100/pthread的内存泄露 # cc thread.c -lpthread# ./a.out1 threads created101 threads created201 threads created301 threads createdERROR, rc is 11, so far 382 threads createdFail:: Resource temporarily unavailable错误提示资源不足,其实是发生了内存泄露,在run函数中,使用pthread_exit退出的线程,并没有像预想的一样释放内存,而是进入僵死状态,只有进程退出的时候才能释放,要想让pthread_exit的线程及时退出,只要在pthread_create后加上pthread_join函数调用即可。只要有了pthread_join,即使没有调用pthread_exit,线程退出时也会释放内存。也就是说pthread_exit只是线程的一个出口,和资源的释放无关,pthread_join直接决定资源是否能够及时释放。*/#include#include void *run(void*p) { pthread_exit(0); //可以不要,会自动调用}int main () { pthread_t thread; int rc; long count = 0; while(1) { if((rc=pthread_create(&thread, NULL, run,NULL))!=0) { printf("ERROR, rc is %d, %ld threads created\n", rc, count); perror("Fail:"); return -1; } //pthread_join(thread,0);//阻塞主线程,等待id线程结束。 if(count++%100==0) printf("%ld threads created\n", count); } return 0;//结束本进程内所有线程}