title: C Pthread: Running only 10 threads simultaneously (what is the problem here)
body: <p>So I'm very new to the whole concept of pthread in C but please hear me out. I have the following code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdbool.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;unistd.h&gt;
  
  
  static pthread_mutex_t  mutex = PTHREAD_MUTEX_INITIALIZER;
  
  static pthread_cond_t   endCond = PTHREAD_COND_INITIALIZER;
  static pthread_cond_t   startCond = PTHREAD_COND_INITIALIZER;
  
  void * threadThingy(void * n){
      pthread_cond_wait(&amp;startCond, &amp;mutex);
      printf("%d: RAND: %d\n", *((int*)n), rand());
  
      //Lock mutex before broadcasting to main thread
      pthread_mutex_lock(&amp;mutex);
      pthread_cond_broadcast(&amp;endCond);
      pthread_mutex_unlock(&amp;mutex);
  
  
      free(n);
      fflush(stdout);
      return 0;
  }
  
  
  int main(void){
      printf("Starting\n");
      pthread_t threads[100];
      int i = 0;
  
      while(i &lt; 10){
          int *arg = malloc(sizeof(int));
          *arg = i;
          pthread_create(&amp;threads[i], NULL, threadThingy, arg);
          i++;
      }
  
      pthread_mutex_lock(&amp;mutex);
      pthread_cond_broadcast(&amp;startCond);
  
      int finished = 0;
  
      while(finished &lt;= 100){
          pthread_cond_wait(&amp;endCond, &amp;mutex);
  
          //Lock mutex so no other requests can come in
          pthread_mutex_lock(&amp;mutex);
          finished++;
  
          int *arg = malloc(sizeof(int));
          *arg = 11;
          pthread_create(threads[i], NULL, threadThingy, arg);
          i++;
          pthread_cond_broadcast(&amp;startCond);
          pthread_mutex_unlock(&amp;mutex);
  
      }
  
      printf("Stopping\n");
  
      sleep(1000);
  }
  </code></pre>
  
  <p>The whole goal is to run (only) 10 threads simultaneously of the 100. My idea was to start 10 threads, than wait until one is finished and start another one. So I let the program wait until a thread returns, then I start a new one so the thread that just returned gets replaced. What have I missed? Because now I only get this as an output: </p>
  
  <blockquote>
    <p>Starting 
    0: RAND: 1804289383</p>
  </blockquote>