pthread_create返回11解决方法

一直以为,程序创建线程,线程运行结束会自动清空资源
最近在一个项目中用到了线程,除去业务逻辑,我把他简化出来是下面这样

//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,所以在线程函数在编译时,需要连接库函数

gcc pthread.c -o pthread -lpthread
运行结果如下
运行结果

不同的机器上最终计数不同,但是结果应该是一样的。
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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值