Max Thread per process. - Linux

This is a discussion on Max Thread per process. - Linux ; 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 = ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Max Thread per process.

  1. 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

  2. Re: Max Thread per process.


    Amit Yadav wrote:

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


    > Error Creating Thread 305 rc is 12


    Sounds like it's out of virtual memory. What is the output of 'ulimit
    -a'? 1MB of virtual memory is needed for each thread's stack by
    default. You can reduce the reserved stack size with
    pthread_attr_setstacksize (when you create the thread).

    Why do you need so many threads? You shouldn't.

    DS


+ Reply to Thread