Pthreads池,保持1000个打开线程,pthread_create()返回11

Pthreads pool, keeping 1000 opened threads, pthread_create() returns 11

本文关键字:create 返回 pthread 线程 保持 1000个 Pthreads      更新时间:2023-10-16

需要一些PTHREADS的帮助。我想在任何时候保持超过1000个线程打开,有点像线程池。下面是代码:

/*
gcc -o test2 test2.cpp -static -lpthread -lstdc++
*/
#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstring>
#include <stdexcept>
#include <cstdlib>
int NUM_THREADS = 2000;
int MAX_THREADS = 100;
int THREADSTACK = 65536;
struct thread_struct{
    int arg1;
    int arg2;
};
pthread_mutex_t mutex_;
static unsigned int thread_count = 0;
string exec(const char* cmd)
{
    int DEBUG=0;
    char buffer[5000];
    string result = "";
    FILE* pipe = popen(cmd, "r");
    if (!pipe && DEBUG) throw runtime_error("popen() failed!");
    try 
    {
        while (!feof(pipe)) 
        {
            if (fgets(buffer, 128, pipe) != NULL)
            {
                result += buffer;
            }
        }
    }
    catch(...)
    {
        pclose(pipe);
        throw;
    }
    pclose(pipe);
    return result;
}

void *thread_test(void *arguments)
{
    pthread_mutex_lock(&mutex_);
    thread_count++;
    pthread_mutex_unlock(&mutex_);
    // long tid;
    // tid = (long)threadid;
    struct thread_struct *args = (thread_struct*)arguments;
    /*
    printf("ARG1=%dn",args->arg1);
    printf("ARG2=%dn",args->arg2);
    */
    int thread_id = (int) args->arg1;
    /*
    int random_sleep;
    random_sleep = rand() % 10 + 1;
    printf ("RAND=[%d]n", random_sleep);
    sleep(random_sleep);
    */
    int random_sleep;
    random_sleep = rand() % 10 + 5;
    // printf ("RAND=[%d]n", random_sleep);
    char command[100];
    memset(command,0,sizeof(command));
    sprintf(command,"sleep %d",random_sleep);
    exec(command);

    random_sleep = rand() % 100000 + 500000;
    usleep(random_sleep);
    // simulation of a work between 5 and 10 seconds
    // sleep(random_sleep);
    // printf("#%d -> sleep=%d total_threads=%un",thread_id,random_sleep,thread_count);

    pthread_mutex_lock(&mutex_);
    thread_count--;
    pthread_mutex_unlock(&mutex_);
    pthread_exit(NULL);
}
int main()
{
    // pthread_t threads[NUM_THREADS];
    int rc;
    int i;
    usleep(10000);
    srand ((unsigned)time(NULL));
    unsigned int thread_count_now = 0;

    pthread_attr_t  attrs;
    pthread_attr_init(&attrs);
    pthread_attr_setstacksize(&attrs, THREADSTACK);
    pthread_mutex_init(&mutex_, NULL);
    for( i=0; i < NUM_THREADS; i++ )
    {
        create_thread:
        pthread_mutex_lock(&mutex_);
        thread_count_now = thread_count;
        pthread_mutex_unlock(&mutex_);
        // printf("thread_count in for = [%d]n",thread_count_now);

        if(thread_count_now < MAX_THREADS)
        {
            printf("CREATE thread [%d]n",i);
            struct thread_struct struct1;
            struct1.arg1 = i;
            struct1.arg2 = 999;
            pthread_t temp_thread;

            rc = pthread_create(&temp_thread, NULL, &thread_test, (void *)&struct1);
            if (rc)
            {
                printf("Unable to create thread %dn",rc);
                sleep(1);
                pthread_detach(temp_thread);
                goto create_thread;
            }

        }
        else
        {
            printf("Thread POOL full %d of %dn",thread_count_now,MAX_THREADS);
            sleep(1);
            goto create_thread;
        }

    }
    pthread_attr_destroy(&attrs);
    pthread_mutex_destroy(&mutex_);
    // pthread_attr_destroy(&attrs);
    printf("Proccess completed!n");
    pthread_exit(NULL);
    return 1;
}

生成300个线程后,它开始给出

错误,从pthread_create()返回的代码是11,之后继续一个接一个地执行它们。

我做错了什么?

根据这个网站,错误码11对应于EAGAIN,这意味着根据这个:

  • 资源不足,无法创建另一个线程。
  • 遇到系统强加的线程数限制。

因此,要解决这个问题,要么创建更少的线程,要么在创建新线程之前等待正在运行的线程完成。

您也可以更改默认线程堆栈大小,参见pthread_attr_setstacksize