Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C pthread_可以创建的最大线程数是多少?_C_Linux_Pthreads - Fatal编程技术网

C pthread_可以创建的最大线程数是多少?

C pthread_可以创建的最大线程数是多少?,c,linux,pthreads,C,Linux,Pthreads,可能重复: 我使用下面的代码测试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_);

可能重复:

我使用下面的代码测试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);
}
这是pthread_create函数可以创建的最大线程数吗

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

# cat /proc/sys/kernel/threads-max
电话号码是772432

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


我的操作系统是Fodaro 16,12核,48G RAM。

根据
pthread\u create(3)
手册页,还有一个限制:

RLIMIT_NPROC soft resource limit (set via setrlimit(2)), which limits the number 
of process  for  a real user ID.
尝试使用
getrlimit(2)
查找此限制的值。如果该值仍然与您测量的数字(825)不匹配,请尝试使用
setrlimit(2)
更改此限制,以验证其是否影响您的测量


编辑:事实上,RLIMIT\u NPROC limit值与通过shell命令
ulimit-u
(打印/设置最大用户进程)获得的值相同。

理论上,进程可以拥有的线程数没有限制。但是实际的限制可能来自所有线程共享资源的事实


这意味着在某个时刻,一个进程不能创建超过一定数量的进程,例如,由于缺乏与此类堆栈空间共享的资源。

每个线程堆栈的默认大小是人为地在测试中施加限制。虽然给定给进程的默认堆栈(初始线程)会根据需要动态增长,但其他线程的堆栈大小是固定的。默认大小通常非常大,大约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);
}
#定义MAXTHREADS 1000000
#定义线程堆栈65536
int main(int argc,char*argv[])
{
pthread_t pid[MAXTHREADS];
pthread_attr_t attrs;
呃,我;
int-cnt=0;
pthread_attr_init(&attrs);
pthread_attr_setstacksize(&attrs,THREADSTACK);
pthread\u mutex\u init(&mutex\u,NULL);
对于(cnt=0;cnt
请注意,
pthread\u create()
调用不会使用
attrs
。把线程属性想象成一个模板,说明
pthread\u create()
应该如何创建线程;它们不是给定给线程的属性。这会让很多有抱负的pthreads程序员绊倒,所以这是你最好一开始就做好的事情之一

至于堆栈大小本身,它必须至少是
PTHREAD\u stack\u MIN
(我相信在Linux中是16384),并且可以被
sysconf(\u SC\u PAGESIZE)
整除。由于页面大小在所有体系结构上都是二的幂,所以使用足够大的二的幂应该总是有效的

此外,我还添加了一个补丁。您只尝试加入一个不存在的线程(循环试图创建但失败的线程),但您需要加入所有线程(以确保它们都完成了任务)

进一步建议的修复方法:


不要使用睡眠,而是使用条件变量。让每个线程在条件变量上等待(
pthread\u cond\u wait()
),然后释放互斥并退出。这样,您的主函数只需要在条件变量上广播(
pthread\u cond\u broadcast()
),告诉所有线程它们现在可以退出,然后它就可以加入每个线程,并且您可以确保该数量的线程确实是并发运行的。现在代码运行时,一些线程可能有足够的时间从睡眠中醒来并退出。

函数
pthread\u create()
只能创建一个线程。你可以多次调用它——显然825没有错误。这可能会随着系统加载、进程更改和其他变量的更改而有所不同。@iccthedral它们不是重复的。他们关注不同的方面和不同的问题。请告诉我为什么需要这么多线程?请告诉我们您观察到了什么
errno
。最后一行应该是
printf(“每个进程的最大线程数=%d\n”,cnt)很可能由于堆栈太多而导致内存不足。在32位机器上,默认堆栈大小为2M,因此825个线程的内存超过1.5 Gig。64位系统使用8M作为堆栈,因此超过了6.5Gig。@Joachim:所以32位版本可能已经耗尽了其中的地址空间,无法分配更多。64位版本可能会继续使用卡车,6.5GB的地址空间很小,因此观察到的限制可能不仅仅是堆栈的总和。理论上,有一个限制:
2^(CHAR\u bit*sizeof(pthread\t)
…@SteveJessop:线程堆栈也会消耗提交费用,而不仅仅是虚拟地址空间。6.5 GB听起来像是
(交换+ram/2)的可能值
,Linux上的默认提交费用限制。默认情况下,Linux不执行严格的提交记帐,但它确实尝试防止严重的过度提交。@R..:hmm,提问者声称有48GB RAM。请注意,
应定义
PTHREAD\u STACK\u MIN
,这是体系结构对thr所需的最小堆栈大小
#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);
}