OT: Limit on maximum # of threads in Linux?
Hi!
I tried the following test program with various 2.4.x und 2.2.x kernels and
noticed that it doesn't *seem* to be possible to create more than 1021
threads. I changed "ulimit -u" from within bash before running the thread
test program, I modified a setting in the /proc filesystem:
/proc/sys/kernel/threads-max
(but that limit is 12287 with a 2.4.21 kernel, so it should be high enough
anyway),
but all I got was:
[...]
Thread #1019: Thread started...thread done
Thread #1020: Thread started...thread done
Thread #1021: Thread started...thread done
Thread #1022: *** Unable to create thread #1021 ***
Why is the limit on the maximum # of threads so low?
Here's the code (can be compiled with "gcc -o thread_test thread_test.c
-lpthread"):
-------------------------------------------------
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void*
thread_func( void* arg )
{
(void) printf( "Thread started..." );
return NULL;
}
int
main( int argc, char** argv )
{
int i = 0;
int retval = 0;
unsigned int sleepsecs = argv[2] ? atoi( argv[2] ) : 3;
long numthreads = 0L;
pthread_t thread_id[ argv[1] ? atol( argv[1] ) : 10 ];
if ( argc < 2 ) {
(void) fprintf( stderr,
"Please specify # of threads to create!\n" );
exit( EXIT_FAILURE );
}
numthreads = atol( argv[1] );
if ( numthreads <= 0 ) {
(void) fprintf( stderr,
"Please specify # of threads > 0\n" );
exit( EXIT_FAILURE );
}
if ( ( retval = setvbuf( stdout, NULL, _IONBF, 0 ) ) != 0 ) {
(void) fprintf( stderr,
"Unable to switch stdout to unbuffered mode!\n" );
exit( EXIT_FAILURE );
}
if ( ( retval = setvbuf( stderr, NULL, _IONBF, 0 ) ) != 0 ) {
(void) fprintf( stderr,
"Unable to switch stderr to unbuffered mode!\n" );
exit( EXIT_FAILURE );
}
(void) printf( "Sleeping %d seconds before creating a new thread\n", sleepsecs );
for ( i = 0; i < numthreads; i++ ) {
(void) printf( "Thread #%d: ", (i + 1) );
if ( ( retval = pthread_create( &thread_id[i], NULL, thread_func, NULL ) ) != 0 ) {
(void) fprintf( stderr,
"*** Unable to create thread #%d ***\n",
i );
}
(void) printf( "thread done\n" );
(void) sleep( sleepsecs );
}
return( EXIT_SUCCESS );
}
--------------------------------------------------------------
Any help will be greatly appreciated!
Greetings,
Holger
Reply to: