What is the maximum number of threads pthread_create can create?

Possible duplicate:
Does thread create pthread_create the same with kernel thread?

I use the following code to check the maximum number of threads that the pthread_create function can create.

#include <pthread.h> #include <stdio.h> static unsigned long long thread_nr = 0; pthread_mutex_t mutex_; void* inc_thread_nr(void* arg) { (void*)arg; pthread_mutex_lock(&mutex_); thread_nr ++; pthread_mutex_unlock(&mutex_); /* printf("thread_nr = %d\n", thread_nr); */ sleep(300000); } int main(int argc, char *argv[]) { int err; int cnt = 0; pthread_t pid[1000000]; pthread_mutex_init(&mutex_, NULL); while (cnt < 1000000) { err = pthread_create(&pid[cnt], NULL, (void*)inc_thread_nr, NULL); if (err != 0) { break; } cnt++; } pthread_join(pid[cnt], NULL); pthread_mutex_destroy(&mutex_); printf("Maximum number of threads per process is = %d\n", thread_nr); } 

And the result:

 Maximum number of threads per process is = 825 

Is this the maximum number of threads that the pthread_create function can create?

In addition, I use the command below to view the maximum number of threads allowed by my system:

 # cat /proc/sys/kernel/threads-max 

And the number 772432.

Why is the output of my program not equal to the value of threads-max ?

My OS is Fodaro 16, with 12 cores, RAM 48G.

+7
source share
3 answers

The default size for the stack over threads artificially sets a limit in your test. Although the default stack assigned to a process (source thread) dynamically grows as needed, the stacks for other threads are fixed in size. The default size is usually very large, something like two megabytes to make sure that the stacks for streams are large enough even for pathological cases (deep recursion, etc.).

In most cases, thread workers require very little stack. I found that on all the architectures I use, a 64k (65536 bytes) stack per thread is sufficient unless I use deep recursive algorithms or large local variables (structures or arrays).

To explicitly indicate the size of the stack in the stream, change your main() to the following:

 #define MAXTHREADS 1000000 #define THREADSTACK 65536 int main(int argc, char *argv[]) { pthread_t pid[MAXTHREADS]; pthread_attr_t attrs; int err, i; int cnt = 0; pthread_attr_init(&attrs); pthread_attr_setstacksize(&attrs, THREADSTACK); pthread_mutex_init(&mutex_, NULL); for (cnt = 0; cnt < MAXTHREADS; cnt++) { err = pthread_create(&pid[cnt], &attrs, (void*)inc_thread_nr, NULL); if (err != 0) break; } pthread_attr_destroy(&attrs); for (i = 0; i < cnt; i++) pthread_join(pid[i], NULL); pthread_mutex_destroy(&mutex_); printf("Maximum number of threads per process is %d (%d)\n", cnt, thread_nr); } 

Note that attrs not consumed by calling pthread_create() . Think that the attributes of the thread are more like the pattern on which pthread_create() should create threads; they are not attributes passed to the stream. This repels many novice pthreads programmers, so this is one of those things that you better get right from the output.

As for the stack size itself, it should be at least PTHREAD_STACK_MIN (16384 on Linux, I think) and is divided by sysconf(_SC_PAGESIZE) . Since page size is the power of two on all architectures, using enough power of the two should always work.

In addition, I added a fix there too. You are trying to join a non-existent thread (the one that the loop tried to create but failed), but you need to join them (to make sure that they all completed their work).

Further recommended fixes:

Instead of using sleep, use a condition variable. Ask each thread to wait ( pthread_cond_wait() ) in the condition variable (while holding the mutex), then release the mutex and exit. Thus, your main function should only broadcast ( pthread_cond_broadcast() ) in the condition variable to report all the threads that they can now exit, then it can join each of them, and you can be sure that this number of threads was simultaneously launched. Since your code is standing now, some threads may have enough time to wake up from sleep and exit.

+6
source

In theory, there is no limit to the number of threads that a process can have. But the practical limitation may be due to the fact that all threads share resources.

This means that at some point the process cannot create more than a certain number of processes due to the lack of resources for sharing with such a stack space, for example.

+3
source

According to the pthread_create(3) man page, there is another limit:

 RLIMIT_NPROC soft resource limit (set via setrlimit(2)), which limits the number of process for a real user ID. 

Try to find the value of this limit with getrlimit(2) . If the value still does not match the number you measured (825), try changing this limit with setrlimit(2) to see if it affects your measurements.

EDIT: In fact, the limit value of RLIMIT_NPROC is the same as the limit value obtained with the ulimit -u shell command (which prints / sets the maximum user processes).

+2
source

All Articles