Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are not a lot of "good" things that can come from playing around with thread stack sizes. Usually when this sort of thing comes up, it's because someone is trying to do create hundreds (or thousands) of threads and running into per process limits. This is generally not a good way to use threads. Example: creating a new thread for every incoming connection request to a server.</p> <p>Even if that's not why you are asking, a few things you should know. </p> <p>In production (real world) code, not a simple example, you can't ever be sure that a specific "reduced" stack size per thread is 100% safe. You can test it extensively at a given stack size and then maybe once you're convinced in your own test environment that it's "large enough", probably increase it slightly (maybe 10-15%) for production use. I don't know of any specific tools that can tell you the peak stack usage per thread at run time. Better off to avoid this completely.</p> <p>Reducing the stack size per thread will not reduce memory consumption directly, or improve performance. You're not optimizing your code by experimenting with this.</p> <p>It's much safer to use the default stack size chosen by the local implementation when you can, until you have a real reason to change it.</p> <p>You were missing some header files for prototypes, had missing return values in the thread funcs, etc. plus some other slight cleanups.</p> <p>Slightly modified version of your code that runs here:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;pthread.h&gt; #include &lt;stdlib.h&gt; #include &lt;assert.h&gt; #include &lt;unistd.h&gt; #include &lt;limits.h&gt; static void *threadFunc1(void *arg) { char arr[5000]; printf("Hello fromt threadFunc1\n"); return NULL; } static void *threadFunc2(void *arg) { char arr[10000]; printf("Hello fromt threadFunc2\n"); return NULL; } int main(int argc, char *argv[]) { pthread_t t1,t2; pthread_attr_t thread_attr; int s = 0; size_t tmp_size=0; s = pthread_attr_init(&amp;thread_attr); assert(s==0); s = pthread_attr_setstacksize(&amp;thread_attr , PTHREAD_STACK_MIN + 0x1000); assert(s==0); s = pthread_attr_getstacksize(&amp;thread_attr , &amp;tmp_size ); assert(s==0); printf("forced stack size of pthread is:%zu\n", tmp_size); s = pthread_create(&amp;t1, &amp;thread_attr, threadFunc1, NULL); assert(s==0); sleep(1); /* not the "right" way, but ok for a quick test */ s = pthread_create(&amp;t2, &amp;thread_attr, threadFunc2, NULL); assert(s==0); sleep(1); printf("Main done()\n"); return 0; } </code></pre> <p>Note: I used </p> <pre><code>$ gcc -O0 -Wall stack_t.c -o stack_t -pthread ./stack_t </code></pre> <p>If you want to experiment with other sizes for the stack, be aware that some implementations require the stacksize to be a multiple of the system page file size. You can try values larger than PTHREAD_STACK_MIN as well obviously, just be aware of that potential issue. </p> <p>EDIT: The example above adds a value to the minimum as an example of increasing it slightly. You may need to add even more. If 0x1000 is not enough, try 0x2000, 0x3000, etc. until you find one that works. As you have discovered, reducing the thread stack size is tricky and can be platform dependent as well. Which is why I have warnings above about it being unsafe.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload