c - 如何使用 pthread_exit() 重用线程

标签 c pthreads

我的计算机允许每个进程 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);
    }


最佳答案

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

Edi 0:

请参阅并阅读:

关于c - 如何使用 pthread_exit() 重用线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14691439/

相关文章:

c - select 未指示 pthread_create 启动的函数中数据到达

c - pthread 在使用虚拟核心时没有提供性能提升

c++ - 如何使派生类函数作为线程的启动例程?

linux - 关于 POSIX 线程上互斥锁的问题

c++ - libusb_get_device_with_vid_pid 上的段错误

c - C 中有趣的增量运算符(在 while 循环中)

c - 执行 malloc 时程序崩溃

当我使用 A* 获取 void * 值并打印它时命令终止

C:如何读取多个字符串片段并打印 #of 片段和 #of 字符

c++ - 使用 g++ 和 clang++ 对 condition_variable 进行虚假唤醒