pthread_create可以创建的最大线程数是多少? [英] What is the maximum number of threads that pthread_create can create?

查看:176
本文介绍了pthread_create可以创建的最大线程数是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
pthread_create创建的线程与内核线程?

我使用下面的代码测试pthread_create函数可以创建的最大线程数.

#include <pthread.h>
#include <stdio.h>

static unsigned long long thread_nr = 0;

pthread_mutex_t mutex_;

void* inc_thread_nr(void* arg) {
    (void*)arg;
    pthread_mutex_lock(&mutex_);
    thread_nr ++;
    pthread_mutex_unlock(&mutex_);

    /* printf("thread_nr = %d\n", thread_nr); */

    sleep(300000);
}

int main(int argc, char *argv[])
{
    int err;
    int cnt = 0;

    pthread_t pid[1000000];

    pthread_mutex_init(&mutex_, NULL);

    while (cnt < 1000000) {

        err = pthread_create(&pid[cnt], NULL, (void*)inc_thread_nr, NULL);
        if (err != 0) {
            break;
        }
        cnt++;
    }

    pthread_join(pid[cnt], NULL);

    pthread_mutex_destroy(&mutex_);
    printf("Maximum number of threads per process is = %d\n", thread_nr);
}

输出为:

Maximum number of threads per process is = 825

pthread_create函数可以创建的最大线程数是吗?

此外,我使用以下命令查看系统允许的最大线程数:

# cat /proc/sys/kernel/threads-max

电话号码是772432.

为什么我的程序输出不等于threads-max的值?

我的操作系统是Fodaro 16,具有12个内核,48G RAM.

解决方案

每个线程堆栈的默认大小是在测试中人为地施加了限制.当分配给进程的默认堆栈(初始线程)根据需要动态增长时,其他线程的堆栈大小是固定的.默认大小通常非常大,大约为2兆字节,以确保每个线程堆栈都足够大,甚至可以用于病理情况(深度递归等).

在大多数情况下,线程工作者只需要很少的堆栈.我发现在我使用的所有体系结构上,只要不使用深度递归算法或大型局部变量(结构或数组),每个线程堆栈就可以使用64k(65536字节)的内存.

要显式指定每个线程的堆栈大小,请将main()修改为如下所示:

#define MAXTHREADS 1000000
#define THREADSTACK  65536

int main(int argc, char *argv[])
{
    pthread_t       pid[MAXTHREADS];
    pthread_attr_t  attrs;
    int  err, i;
    int  cnt = 0;

    pthread_attr_init(&attrs);
    pthread_attr_setstacksize(&attrs, THREADSTACK);

    pthread_mutex_init(&mutex_, NULL);

    for (cnt = 0; cnt < MAXTHREADS; cnt++) {

        err = pthread_create(&pid[cnt], &attrs, (void*)inc_thread_nr, NULL);
        if (err != 0)
            break;
    }

    pthread_attr_destroy(&attrs);

    for (i = 0; i < cnt; i++)
        pthread_join(pid[i], NULL);

    pthread_mutex_destroy(&mutex_);

    printf("Maximum number of threads per process is %d (%d)\n", cnt, thread_nr);
}

请注意,attrs不会被pthread_create()调用占用.线程属性更像是pthread_create()应该如何创建线程的模板. 它们不是赋予线程的属性.这绊倒了许多有抱负的pthread程序员,所以这是您最好从一开始就做好的事情之一.

关于堆栈大小本身,它必须至少为PTHREAD_STACK_MIN(我相信在Linux中为16384)并且可以被sysconf(_SC_PAGESIZE)整除.由于页面大小在所有体系结构上都是2的幂,因此始终应该使用足够大的2的幂.

此外,我也在其中添加了一个修复程序.您只能尝试加入一个不存在的线程(该循环尝试创建一个但失败的线程),但是您需要加入所有线程(以确保它们都已完成工作).

其他推荐的修复程序:

使用条件变量来代替睡眠.让每个线程在条件变量上等待(pthread_cond_wait())(同时保持互斥锁),然后释放互斥锁并退出.这样,您的主要功能只需要在条件变量上广播(pthread_cond_broadcast())即可告知所有线程现在可以退出,然后可以将其加入每个线程,并且可以确保确实有多个线程在同时运行.按照您的代码现在的样子,某些线程可能有足够的时间从睡眠中唤醒并退出.

Possible Duplicate:
The thread create by pthread_create the same with the kernel thread?

I use the code below to test the maximum number of threads that the pthread_create function can create.

#include <pthread.h>
#include <stdio.h>

static unsigned long long thread_nr = 0;

pthread_mutex_t mutex_;

void* inc_thread_nr(void* arg) {
    (void*)arg;
    pthread_mutex_lock(&mutex_);
    thread_nr ++;
    pthread_mutex_unlock(&mutex_);

    /* printf("thread_nr = %d\n", thread_nr); */

    sleep(300000);
}

int main(int argc, char *argv[])
{
    int err;
    int cnt = 0;

    pthread_t pid[1000000];

    pthread_mutex_init(&mutex_, NULL);

    while (cnt < 1000000) {

        err = pthread_create(&pid[cnt], NULL, (void*)inc_thread_nr, NULL);
        if (err != 0) {
            break;
        }
        cnt++;
    }

    pthread_join(pid[cnt], NULL);

    pthread_mutex_destroy(&mutex_);
    printf("Maximum number of threads per process is = %d\n", thread_nr);
}

And the output is :

Maximum number of threads per process is = 825

Is that the maximum number of threads that the pthread_create function can create?

Besides, I use the command below to view the maximum number of threads my system allows:

# cat /proc/sys/kernel/threads-max

And the number is 772432.

Why is the output of my program not equal to the value of threads-max ?

My OS is Fodaro 16, with 12 cores, 48G RAM.

解决方案

The default size for the per-thread stack is artificially imposing the limit in your test. While the default stack given to the process (the initial thread) grows dynamically as needed, the stacks for the other threads are fixed in size. The default size is usually extremely large, something like two megabytes, to make sure the per-thread stack is large enough for even the pathological cases (deep recursion and so on).

In most cases, thread workers need very little stack. I've found that on all architectures I've used, 64k (65536 bytes) per-thread stack is sufficient, as long as I don't use deeply recursive algorithms or large local variables (structures or arrays).

To explicitly specify a per-thread stack size, modify your main() to something like the following:

#define MAXTHREADS 1000000
#define THREADSTACK  65536

int main(int argc, char *argv[])
{
    pthread_t       pid[MAXTHREADS];
    pthread_attr_t  attrs;
    int  err, i;
    int  cnt = 0;

    pthread_attr_init(&attrs);
    pthread_attr_setstacksize(&attrs, THREADSTACK);

    pthread_mutex_init(&mutex_, NULL);

    for (cnt = 0; cnt < MAXTHREADS; cnt++) {

        err = pthread_create(&pid[cnt], &attrs, (void*)inc_thread_nr, NULL);
        if (err != 0)
            break;
    }

    pthread_attr_destroy(&attrs);

    for (i = 0; i < cnt; i++)
        pthread_join(pid[i], NULL);

    pthread_mutex_destroy(&mutex_);

    printf("Maximum number of threads per process is %d (%d)\n", cnt, thread_nr);
}

Note that attrs is not consumed by the pthread_create() call. Think of the thread attributes more like a template on how pthread_create() should create the threads; they are not attributes given to the thread. This trips up many aspiring pthreads programmers, so it's one of those things you'd better get right from the get go.

As to the stack size itself, it must be at least PTHREAD_STACK_MIN (16384 in Linux, I believe) and divisible by sysconf(_SC_PAGESIZE). Since page size is a power of two on all architectures, using a large enough power of two should always work.

Also, I added a fix in there, too. You only try to join a nonexistent thread (the one that the loop tried to create, but failed), but you need to join all of them (to make sure they're all done their job).

Further recommended fixes:

Instead of using a sleep, use a condition variable. Have each thread wait (pthread_cond_wait()) on the condition variable (while holding the mutex), then release the mutex and exit. That way your main function only needs to broadcast (pthread_cond_broadcast()) on the condition variable to tell all threads they can now exit, then it can join each one, and you can be sure that that number of threads were really concurrently running. As your code stands now, some threads may have enough time to wake up from the sleep and exit.

这篇关于pthread_create可以创建的最大线程数是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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