【问题标题】:How to reuse threads using pthread_exit()如何使用 pthread_exit() 重用线程
【发布时间】:2013-02-04 16:52:29
【问题描述】:

我的计算机允许每个进程有 380 个线程,这对我来说很好。我没有问题 当我调用 380 次函数 sdfpthread_create() 时。但连续调用返回 错误 11(资源暂时不可用)。

明显的解决方法是使用pthread_exit(),但是我没有解决问题, 限制仍然是创建 380 个线程。

如何重用线程?

#include <stdio.h> #include <string.h> #include <pthread.h> #include <stdlib.h> #include <unistd.h> void *doSomeThing() { sleep(99); pthread_exit(NULL); } int main(void) { pthread_t tid; int i; int err; /* Create threads */ for (i=0; i<380; ++i) { err = pthread_create(&tid, NULL, doSomeThing, NULL); if (err != 0) printf("\n1) Can't create thread :[%s]", strerror(err)); } sleep(1); /* Reuse threads */ for (i=0; i<5; ++i) { err = pthread_create(&tid, NULL, doSomeThing, NULL); if (err != 0) printf("\n2) Can't create thread :[%s]", strerror(err)); } exit(0); }

【问题讨论】:

  • 这不是很清楚。你是说上面的代码有效还是无效?

标签: c pthreads


【解决方案1】:

您需要调用pthread_join(3) 来真正清理线程退出状态,或者创建不可连接的线程。

编辑 0:

请查看并阅读

【讨论】:

  • 我使用了 pthread_detach(pthread_self());我没有问题。您的帮助非常有用。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-04
  • 2012-08-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多