开发者

Return code from pthread_create() is 11

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;                         开发者_StackOverflow 
   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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜