如何使用了pthread_exit重用线程() [英] How to reuse threads using pthread_exit()

查看:133
本文介绍了如何使用了pthread_exit重用线程()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的电脑允许每个过程,这是对我罚款380线程。我没有问题
当我打电话380次功能sdfpthread_create()。但连续调用返回
埃罗11(资源暂时不可用)。

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

我如何可以重用线程?

     &#35include<&stdio.h中GT;
    &#35include<&string.h中GT;
    &#35include< pthreads.h中>
    &#35include<&stdlib.h中GT;
    &#35include<&unistd.h中GT;
    无效* DoSomething的()
    {
        睡眠(99);
        了pthread_exit(NULL);
    }    INT主要(无效)
    {
        TID的pthread_t;
        INT I;
        INT犯错;        / *创建线程* /
        对于(i = 0; I< 380; ++ I){
            ERR =在pthread_create(TID,NULL,DoSomething的,NULL);
            如果(ERR!= 0)
                的printf(\\ N1)无法创建线程:[%S]字符串错误(ERR));
        }        睡眠(1);        / *重用线程* /
        对于(I = 0; I&小于5 ++ⅰ){
            ERR =在pthread_create(TID,NULL,DoSomething的,NULL);
            如果(ERR!= 0)
                的printf(\\ N2)无法创建线程:[%S]字符串错误(ERR));
        }        出口(0);
    }


解决方案

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

艾迪0:

请参阅和读取的:

My computer allows 380 threads per process, which is fine for me. I have no problem when I call 380 times to function sdfpthread_create (). But successive call returns the erro 11 (Resource temporarily unavailable).

The apparent solution is to use the pthread_exit (), but I did not solve the problem, the limit is still 380 threads created.

How I can reuse thread?



    #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);
    }


解决方案

You need to call pthread_join(3) to actually cleanup thread exit state, or create threads that are not joinable.

Edi 0:

Please see and read:

这篇关于如何使用了pthread_exit重用线程()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆