一直以为,程序创建线程,线程运行结束会自动清空资源
实则不然。
//pthread.c 错误demo示例
#include <stdio.h>
#include <pthread.h>
static int testcount = 0;
static void *test_thread_handler()
{
testcount++;
printf("%d\n",testcount);
return 0;
}
int main( int argc, char *argv[] )
{
int ret = 0;
pthread_t test_tid;
while(1)
{
usleep(10000);
ret = pthread_create(&test_tid, NULL, test_thread_handler,NULL);
if(ret != 0)
{
printf("Create handler error :%d!\t testcount:%d\n", ret, testcount);
return -1;
}
}
return 0;
}
备注:pthread库不是Linux系统默认的库,连接时需要使用静态库libpthread.a,所以在线程函数在编译时,需要连接库函数
pthread_create()返回11的错误码表示Resource temporarily unavailable
资源暂时不可用,按理说线程return 0后资源应该自动释放,同时我使用free查看发现内存也是足够的。
经过多方面查找资料,得知linux线程执行和windows不同,pthread有两种状态joinable状态和unjoinable状态,默认的状态是joinable。
如果线程是joinable状态,当线程函数自己返回退出时或pthread_exit时都不会释放线程所占用堆栈和线程描述符(总计8K多),它的状态类似于进程中的Zombie Process(僵尸进程)。只有当调用了pthread_join之后这些资源才会被释放。
若是unjoinable状态的线程,这些资源在线程函数退出时或pthread_exit时自动会被释放。
但是调用pthread_join(pthread_id)后,如果该线程没有运行结束,调用者会被阻塞,如果不需要阻塞的情况下,这时可以在子线程中加入代码
pthread_detach(pthread_self())
或者父线程调用
pthread_detach(test_tid)(非阻塞,可立即返回)
这将该子线程的状态设置为detached,则该线程运行结束后会自动释放所有资源。
最终程序如下:
//pthread.c 修复错误demo后示例
#include <stdio.h>
#include <pthread.h>
static int testcount = 0;
static void *test_thread_handler()
{
pthread_detach(pthread_self());
testcount++;
printf("%d\n",testcount);
pthread_exit(0);
return 0;
}
int main( int argc, char *argv[] )
{
pthread_t test_tid;
int ret = 0;
while(1)
{
usleep(10000);
ret = pthread_create(&test_tid, NULL, test_thread_handler,NULL);
if(ret != 0)
{
printf("Create handler error :%d!\t testcount:%d\n", ret, testcount);
return -1;
}
}
return 0;
}