How many threads can we create?


I was asked this question during the lab, because one lab group could only create a few hundred threads. Here comes a discussion.

My first try was to see how the system was configured.

% getconf PTHREAD_THREADS_MAX
undefined


So, perhaps there is no limit, apart from available memory.  If we have the simplest possible thread the memory that is required, is essentially the thread's stack. So is memory the limiting factor? Yes, which can be seen by the following piece of code:

  for(i = 0; i < N_THREADS; i++)
    if( pthread_create(&thread_id[i], NULL, dotprod, (void *) i) ) {
      perror("Error in pthread_create");
      exit(1);
    }


If N_THREADS is too large we get the following message (from the perror-routine):

  Error in pthread_create: Cannot allocate memory

So let us find out the size of the stack. Some pthreads implementations implement two functions, pthread_attr_getstacksize and pthread_attr_setstacksize with which one can find out (and change) the stack size for a thread. These routines are available if the symbolic constant _POSIX_THREAD_ATTR_STACKSIZE is defined. It is not defined in our implementation.

Here is the next try. I am using tcsh and the limit command prints the limits of certain quantities (in bash you can type ulimit -a).

% limit
cputime      unlimited
filesize     unlimited
datasize     unlimited
stacksize    10240 kbytes
coredumpsize 0 kbytes
memoryuse    unlimited
vmemoryuse   unlimited
descriptors  1024
memorylocked 32 kbytes
maxproc      16383


So the stack requires 10240 kbyte, i.e. 10 Mbyte, quite a lot. Let us change the size of the stack (using limit or ulimit) and create a maximum number of thread (for each size of the stack). Here comes a table. memory is the product of num_threads and stack_size.

num_threads    stack_size         memory
                (kbyte)          (Gbyte)

      23779         128            2.90
      12081         256            2.95
       6083         512            2.97
       3052        1024            2.98
       1528        2048            2.98
        763        4096            2.98
        379        8192            2.96
        189       16384            2.95
         93       32768            2.91
         45       65536            2.81
         21      131072            2.62


So, the amount of memory is fairly constant, about 3 Gbyte which is how much virtual memory I have in total. Running on a machine with more memory I can easily create 30000 threads (with small stacks).