linux 多线程 pthread_create返回11,pthread_create返回11解决方法

pthread_create返回11解决方法

pthread_create返回11解决方法

pthread_create 返回11 说明资源不可用。通常是线程资源没有释放导致的。

用ulimit -a 可以查看系统线程最大数。

原因

pthread有两种状态joinable状态和unjoinable状态,默认的状态是joinable。这种状态下不会自己释放。

解决

线程执行完毕添加 pthread_detach(pthread_self())  函数。

以下是原文

pthread_create返回11解决方法

https://blog.csdn.net/cry1994/article/details/52649520

一直以为,程序创建线程,线程运行结束会自动清空资源

最近在一个项目中用到了线程,除去业务逻辑,我把他简化出来是下面这样

//pthread.c 错误demo示例

#include

#include

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;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

备注:pthread库不是Linux系统默认的库,连接时需要使用静态库libpthread.a,所以在线程函数在编译时,需要连接库函数

gcc pthread.c -o pthread -lpthread

运行结果如下

97098157064dbec815732523c91c378f.png

不同的机器上最终计数不同,但是结果应该是一样的。

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

#include

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;

}

pthread_create返回11解决方法相关教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
多线程中的pthread_create()函数是用来创建一个新的线程的。它的函数原型为int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)。这个函数通过传入四个参数来创建新的线程。第一个参数thread是一个指向pthread_t类型的指针,用来保存新线程的ID。第二个参数attr是一个指向pthread_attr_t类型的指针,用来指定新线程的属性,如果为NULL则使用默认属性。第三个参数start_routine是一个函数指针,指向新线程将要执行的函数。最后一个参数arg是一个指向任意类型的指针,用来传递给start_routine函数的参数。调用成功返回0,失败返回错误编号。 每个线程都有自己的独立栈来保存线程运行时形成的临时数据。而上下文中保存的是CPU调度时存放在寄存器中的临时数据。线程的标识符类型为pthread_t,可以使用pthread_self()函数获取当前线程的ID,类似于使用getpid()函数获取进程的ID。[2,3]<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Linux线程:创建(pthread_create),等待(pthread_join),退出(pthread_exit)](https://blog.csdn.net/m0_74985965/article/details/128815940)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值