C语言 pthread_create() 的返回码是 11

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7038586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:22:40  来源:igfitidea点击:

Return code from pthread_create() is 11

cmultithreadinggcc

提问by Jaseem

I am trying to run a simple multi threaded programming and i am getting this error from gcc

我正在尝试运行一个简单的多线程编程,但我从 gcc 收到此错误

return code from pthread_create() is 11

pthread_create() 的返回码是 11

how do i solve this issue ?

我该如何解决这个问题?

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

#define NUM_THREADS     20000                           

void *PrintHello(void *threadid)                            
{                           
   long tid;                            
   tid = (long)threadid;                            
   printf("Hello World! It's me, thread #%ld!\n", tid);                         
   pthread_exit(NULL);                          
}                           

int main (int argc, char *argv[])                           
{                           
   pthread_t threads[NUM_THREADS];                          
   int rc;                          
   long t;                          
   for(t=0; t<NUM_THREADS; t++){                            
      printf("In main: creating thread %ld\n", t);                          
      rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);                            
      if (rc){                          
         printf("ERROR; return code from pthread_create() is %d\n", rc);                            
         exit(-1);                          
      }                         
   }                            

   /* Last thing that main() should do */                           
   pthread_exit(NULL);                          
}                           

回答by Christian.K

Well, you could start with determining what the error actually means. According to thisand this(other resources will tell you the same information, this is just an example), the number 11 stands for EAGAINwhich in turn means "The system lacked the necessary resources to create another thread, or the system-imposed limit on the total number of threads in a process PTHREAD_THREADS_MAX would be exceeded.".

好吧,您可以从确定错误的实际含义开始。根据thisthis(其他资源会告诉你同样的信息,这只是一个例子),数字11代表EAGAIN它依次表示“系统缺乏创建另一个线程所需的资源,或者系统对线程施加的限制将超过进程 PTHREAD_THREADS_MAX 中的线程总数。”。

That matches the fact that your are trying to create 20.000(!) threads. Create less threads, or wait until threads complete before starting new ones.

这与您尝试创建 20.000(!) 个线程的事实相符。创建更少的线程,或者等到线程完成后再开始新的线程。

Note that the maximum number of threads that can be created depends on your system (and possibly even depends on a number of other settings). Google for "How to Find PTHREAD_THREADS_MAX" if you really need to know.

请注意,可以创建的最大线程数取决于您的系统(甚至可能取决于许多其他设置)。如果您真的需要知道,请谷歌搜索“如何查找 PTHREAD_THREADS_MAX”。

However, unless this is just a trivial example for toying around (or maybe even then) you might want to look into the concept of thread pools and queues.

但是,除非这只是一个玩弄(甚至可能那时)的简单示例,否则您可能想要研究线程池和队列的概念。