Threads and stack size

Can someone explain me, why when I create new threads with stack bigger than default (using pthread_attr_setstacksize()) i can create more? Each thread has it's own stack, so how bigger stack can cause possibility to create more threads?

Could you post some example code and give info on the O/S ?
Thx

#include <stdio.h>
#include <pthread.h>

#define MODIFY_STACK 

void *fnc(void *a)
{
        sleep(99999);
        return NULL;
}

int main()
{
        int i = 0;
        pthread_t abc;
#ifdef MODIFY_STACK
        pthread_attr_t    attr;
#endif
        
#ifdef MODIFY_STACK
        pthread_attr_init(&attr);
        pthread_attr_setstacksize(&attr, 20000);
#endif
        
#ifdef MODIFY_STACK
        while (pthread_create(&abc, &attr, fnc, NULL) == 0) {
#else
        while (pthread_create(&abc, NULL, fnc, NULL) == 0) {
#endif
                i++;
        }
        
        printf("%d\n", i);
        fflush(stdout);
        return 0;
}

I use linux 32bit. When I compile this code without MODIFY_STACK definition I can create 380 threads. When I compile with it, I am able to open about 30000 threads. I want just know, how it is possible. On linux 64bit, MODIFY_STACK doesn't make any difference (stack is bigger by default)

You're giving it an invalid stack size of 20000 and not checking if it returns success or error so there's no telling what it's actually setting the stack size to.

From man pthread_attr_setstacksize:

ERRORS
       pthread_attr_setstacksize() can fail with the following error:

       EINVAL The stack size is less than PTHREAD_STACK_MIN (16384) bytes.

       On  some  systems,  pthread_attr_setstacksize() can fail with the error
       EINVAL if stacksize is not a multiple of the system page size.

Try 20480, that's an even multiple of an x86 system's page size of 4096.

You should also check what the default thread size actually is on your system; whatever it is, I'm pretty sure 20000 is smaller. It used to be 64K, but may have been expanded a few years ago -- some buggy and piggy but unfortunately popular window managers were overrunning default 64K stacks with no obvious way to fix it, and there was talk of upping it to a meg to accommodate their poor code.

You're also never forcing your threads to join or exit, which can cause undefined behavior.

Also take a look at pthread_attr_setguardsize.

---------- Post updated at 03:03 PM ---------- Previous update was at 02:40 PM ----------

64-bit linux has a much larger limit regardless of the actual size of the thread stacks because it has 2^32 times more address space to waste. You can waste gigabytes upon gigabytes of virtual memory in a 64-bit system as long as you don't actually try to use it all. You could say the main advantage of a 64-bit system is a much bigger imagination :wink:

This code was just an example. I just wanted to know, why bigger stack can allow to create more threads than default

On my system the default stack size is 8388608.
So setting a stack of 20000 would allow many more threads to be created.

If you're comparing 64-bit linux to 32-bit linux, you might have missed the explanation I tacked on at the last minute. Otherwise, I think you're just mistaken about your system's default thread size, or having problems with your code that my crystal ball missed.

If you're checking compiled-in constants for the default thread size, keep in mind they're often actually minimums, you should get the default by querying the library at runtime. Try pthread_attr_init then pthread_attr_getstacksize

Default stack size with NPTL on Linux 32 bits is 8Mb IIRC.