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

时间:2016-10-01 12:32:49

标签: c++ multithreading

需要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=%d\n",args->arg1);
    printf("ARG2=%d\n",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=%u\n",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 %d\n",rc);

                sleep(1);
                pthread_detach(temp_thread);
                goto create_thread;
            }


        }
        else
        {
            printf("Thread POOL full %d of %d\n",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,然后逐个执行它们。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

根据此website,错误代码11对应于EAGAIN,这意味着根据this

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

因此,要解决您的问题,请在创建新线程之前创建更少的线程或等待运行的线程完成。

您还可以更改默认线程堆栈大小,请参阅pthread_attr_setstacksize