Max Thread per process.



I am trying to run a simple program which creates parallel threads. The thread function contains a simple printf. The program is given below.

int main()
{
#define MAX_THREAD 310
pthread_t thread[MAX_THREAD];
int i = 0 ;
int rc = 0;
int status = 0;
for ( i = 0; i< MAX_THREAD; i++ )
{
printf("\n Creating Thread %d\n",i);
rc = pthread_create(&thread[i],NULL,(void *)printsome,NULL);
if ( rc )
{
printf("\n Error Creating Thread %d rc is %d %S\n",i);
exit(-1);
}
}

for ( i = 0; i< MAX_THREAD; i++ )
{
printf("\n Waiting for Thread %d\n",i);
rc = pthread_join(thread[i],(void *)(&status));
if ( rc )
{
printf("\n Error Joining Thread %s\n",i);
exit(-1);
}
printf("\n Thread %d Done\n",i);
}

return;

}
void printsome()
{
printf("\n Bravo !!!");
sleep(2);
}

The program creates about 304 threads and the prints the following error:

Creating Thread 302

Creating Thread 303

Creating Thread 304

Bravo !!!
Bravo !!!
Bravo !!!
Bravo !!!
Bravo !!!
Bravo !!!
Bravo !!!
Creating Thread 305

Error Creating Thread 305 rc is 12

Executing the same program again and again gives the same result. I am not sure why is it failing to create more then 305 thread ? The number is much lower then Linux limits or glibc PTHREAD_THREADS_MAX limit. Also the rc value 12 is a little strange because pthread_create should not return that.

I am running 2.6.9 kernel. Any pointer on where this limit is coming from ?


Thanks

Amit
.