pthread_create() blocks if maxthreads reached?

Craig Rodrigues rodrigc at crodrigues.org
Sat Jan 31 22:13:07 PST 2004


Hi,

I wrote the following small test program to spawn
a large number of threads.

==================================================================

#include <sys/types.h>
#include <sys/select.h>
#include <pthread.h>
#include <stdio.h>

#define NUM 100000

void *thr(void *p)
{
  select(0, NULL, NULL, NULL, NULL);  
  return p;
}


int
main()
{
  pthread_t t[NUM];

  int ret=0;
  int i;
  
  for(i=0; ; ++i)
  {
     printf("Entering pthread_create...\n");
     ret = pthread_create(&t[i], NULL, thr, NULL);
     printf("Left pthread_create...\n");

     if( ret != 0 ) {
         printf("%d %s\n", ret, strerror(ret)); 
         break;
     }
  }
  printf("Max threads reached at: %d\n", i);

  return 0;
}
==============================================================


If I link this program with -lthr or -lc_r, the
program will terminate.

If I link with -lpthread, the program seems to
block in the pthread_create() call, instead of
returning EAGAIN (to indicate that a new thread cannot
be created due to hitting some system limit). 

Any ideas on this?

Thanks.
-- 
Craig Rodrigues        
http://crodrigues.org
rodrigc at crodrigues.org


More information about the freebsd-threads mailing list