Subj : increasing the number of threads by reducing their stack size To : comp.programming.threads From : emarinova Date : Tue Aug 09 2005 11:44 am Hi, I am trying to increase the max number of threads that can be created by a program by decreasing the stack size. the OS is Linux 2.4.20-8 (red hat )with LinuxTreads. It works if I decrease the stack size using the shell command ulimit or limit (bash shell) and then run my program - my program is able to create more threads. What does not work is if I want to reduce the stack size programatically from the program itself using setrlimit( RLIMIT_STACK, &limits ). The stack size changes, but the maximum number of threads does not increase. There is no function pthread_attr_setstacksize in this implementation of LinuxThread. I tried everything I can think of - used parent and a child - the parent modifies the stack size and the child creates the threads - the maxumum number of threads does not get increased. I also have download the source of tsh to see if I do anything wrong. It apears that the shell does the same - uses setrlimit RLIMIT_STACK. I am obviously doing something wrong. My code is bellow, any ideas? //compilation: g++ -lpthread -o threads threads.c #include #include #include #include #include #include #include #include #include #define MAX_THREADS 10000 #define STACK_SIZE_ 1048576 int set_stacksize_system( const rlim_t& soft_limit ); void* run(void*) { sleep(60 * 60); return NULL; } int set_stacksize_system( const rlim_t& soft_limit ) { int result = 0; struct rlimit limits; /*Setting a low stack size hoping to be able to create more threads */ if ( -1 == getrlimit( RLIMIT_STACK, &limits ) ) { printf("%s %s\n", "Cannot obtain the current stack size from the system. Reson: ", strerror(errno) ); } else { printf("old limit: %d\n", limits.rlim_cur ); limits.rlim_cur = soft_limit; limits.rlim_max = soft_limit; if ( -1 == setrlimit( RLIMIT_STACK, &limits ) ) { printf( "%s %s\n", "Cannot set the current stack size limit. Reson: ", strerror(errno) ); }else { getrlimit( RLIMIT_STACK, &limits ); printf("new limit: %d\n", limits.rlim_cur ); result = 1; } } return result; } int main(int argc, char *argv[]) { int rc = 0, i = 0; rlim_t soft_limit = STACK_SIZE_; set_stacksize_system( soft_limit ); pthread_t thread[MAX_THREADS]; printf("starting to create threads\n"); for (i = 0; i < MAX_THREADS && rc == 0; i++) { rc = pthread_create(&(thread[i]), NULL, /*(void *) */&run, NULL); if (rc == 0) { pthread_detach(thread[i]); if ((i + 1) % 100 == 0) printf("thread number %i created\n", i + 1); } else { printf("Failed with return code %i creating thread %i.\n", rc, i + 1); } } return 0; } .