Note that there are some explanatory texts on larger screens.

plurals
  1. PONPTL caps maximum threads at 65528?
    primarykey
    data
    text
    <p>The following code is supposed to make 100,000 threads:</p> <pre><code>/* compile with: gcc -lpthread -o thread-limit thread-limit.c */ /* originally from: http://www.volano.com/linuxnotes.html */ #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;unistd.h&gt; #include &lt;pthread.h&gt; #include &lt;string.h&gt; #define MAX_THREADS 100000 int i; void run(void) { sleep(60 * 60); } int main(int argc, char *argv[]) { int rc = 0; pthread_t thread[MAX_THREADS]; printf("Creating threads ...\n"); for (i = 0; i &lt; MAX_THREADS &amp;&amp; rc == 0; i++) { rc = pthread_create(&amp;(thread[i]), NULL, (void *) &amp;run, NULL); if (rc == 0) { pthread_detach(thread[i]); if ((i + 1) % 100 == 0) printf("%i threads so far ...\n", i + 1); } else { printf("Failed with return code %i creating thread %i (%s).\n", rc, i + 1, strerror(rc)); // can we allocate memory? char *block = NULL; block = malloc(65545); if(block == NULL) printf("Malloc failed too :( \n"); else printf("Malloc worked, hmmm\n"); } } sleep(60*60); // ctrl+c to exit; makes it easier to see mem use exit(0); } </code></pre> <p>This is running on a 64bit machine with 32GB of RAM; Debian 5.0 installed, all stock.</p> <ul> <li>ulimit -s 512 to keep the stack size down </li> <li>/proc/sys/kernel/pid_max set to 1,000,000 (by default, it caps out at 32k pids). </li> <li>ulimit -u 1000000 to increase max processes (don't think this matters at all) </li> <li>/proc/sys/kernel/threads-max set to 1,000,000 (by default, it wasn't set at all)</li> </ul> <p>Running this spits out the following:</p> <pre><code>65500 threads so far ... Failed with return code 12 creating thread 65529 (Cannot allocate memory). Malloc worked, hmmm </code></pre> <p>I'm certainly not running out of ram; I can even launch several more of these programs all running at the same time and they all start their 65k threads.</p> <p>(Please refrain from suggesting I not try to launch 100,000+ threads. This is simple testing of something which <em>should</em> work. My current epoll-based server has roughly 200k+ connections at all times and <a href="http://www.usenix.org/events/hotos03/tech/full_papers/vonbehren/vonbehren_html/index.html" rel="nofollow noreferrer">various papers</a> would suggest that threads just might be a better option. - Thanks :) )</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.
 

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