返回$从pthread_create的C $ C()是11 [英] Return code from pthread_create() is 11

查看:776
本文介绍了返回$从pthread_create的C $ C()是11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行一个简单的多线程编程,我正从GCC这个错误


  

收益$ C $从在pthread_create()c为11


我该怎么解决这个问题呢?

 的#include< pthreads.h中>
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;#定义NUM_THREADS 20000无效* PrintHello(void *的主题ID)
{
   长TID;
   TID =(长)主题ID;
   的printf(你好!世界是我,线#%LD \\ n!,TID);
   了pthread_exit(NULL);
}INT主(INT ARGC,CHAR *的argv [])
{
   的pthread_t线程[NUM_THREADS];
   INT RC;
   长吨;
   对于(t = 0; T< NUM_THREADS;吨++){
      的printf(在主:创建线程%LD \\ n,T);
      RC =在pthread_create(安培;螺纹[T],NULL,PrintHello,(无效*)T);
      如果(RC){
         的printf(ERROR;从pthread_create的收益code()为%d \\ n,RC);
         出口(-1);
      }
   }   / *最后一件事是主要的()应该做的* /
   了pthread_exit(NULL);
}


解决方案

那么,你可以确认哪些错误实际上意味着开始。根据这个并的这个(其他资源会告诉你同样的信息,这只是一个例子),11号看台上为 EAGAIN 这反过来又意味着系统缺乏必要的资源,以创建另一个线程或进程PTHREAD_THREADS_MAX将超过上的线程总数系统强加的限制。

这符合你试图创建20.000(!)线程的事实。创建线程少,或者等到线程开始新的之前完成。

需要注意的是,可以创建的线程的最大数量取决于在系统上(甚至可能取决于许多的其他设置)。谷歌如何找到PTHREAD_THREADS_MAX如果你真的需要知道的。

然而,除非这只是玩弄左右(或者甚至再)一个简单的例子,你可能想看看线程池和队列的。

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

return code from pthread_create() is 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);                          
}                           

解决方案

Well, you could start with determining what the error actually means. According to this and this (other resources will tell you the same information, this is just an example), the number 11 stands for EAGAIN which 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.".

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.

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.

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.

这篇关于返回$从pthread_create的C $ C()是11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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