Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;                          
   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);                          
}                           
like image 793
Jaseem Avatar asked Aug 12 '11 10:08

Jaseem


People also ask

What is the return value of pthread_create?

pthread_create() returns zero when the call completes successfully. Any other return value indicates that an error occurred. When any of the following conditions are detected, pthread_create() fails and returns the corresponding value.

Which value does pthread_create () function return if it is successfully executed?

On success, pthread_create() returns 0; on error, it returns an error number, and the contents of *thread are undefined.

What is pthread_create in C?

DESCRIPTION. The pthread_create() function is used to create a new thread, with attributes specified by attr, within a process. If attr is NULL, the default attributes are used. If the attributes specified by attr are modified later, the thread's attributes are not affected.


1 Answers

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.

like image 112
Christian.K Avatar answered Sep 24 '22 15:09

Christian.K