pthread_create() blocks if maxthreads reached?

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


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;

  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)); 
  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?

Craig Rodrigues
rodrigc at

More information about the freebsd-threads mailing list