I have a program which accepts 2 N-digit numbers, multiplies them using threads & prints the output.
The number of threads created here are 2 * N - 1.
whenever I run the program for N > 151, the program gives me a segmentation fault.
Is there a cap on maximum number of threads a process can get from the thread pool?
If so, could this be a valid reason for the fault?
Edit:
Valgrind finds no memory leaks for N <= 150.
I’m running the program in Linux 2.6.x kernel.
By default, each thread gets an 8MB stack. 300 threads by 8MB is 2.4GB just for thread stacks – if you’re running in 32 bit mode, then that’s probably most of your allowed process address space.
You can use
pthread_attr_setstacksize()to reduce the size of your thread stacks to something a bit more sane before you create them:(Create a new
pthread_attr, set the stack size then pass that topthread_create).