title: multi threaded FastCGI App
body: <p>I want to write a FastCGI app which should handle multiple simultaneous requests using threads. I had a look at the <a href="https://github.com/luislavena/fcgi/blob/master/examples/threaded.c" rel="noreferrer">threaded.c</a> sample which comes with the SDK:</p>
  
  <pre><code>#define THREAD_COUNT 20
  static int counts[THREAD_COUNT];
  
  static void *doit(void *a)
  {
      int rc, i, thread_id = (int)a;
      pid_t pid = getpid();
      FCGX_Request request;
      char *server_name;
  
      FCGX_InitRequest(&amp;request, 0, 0);
  
      for (;;)
      {
          static pthread_mutex_t accept_mutex = PTHREAD_MUTEX_INITIALIZER;
          static pthread_mutex_t counts_mutex = PTHREAD_MUTEX_INITIALIZER;
  
          /* Some platforms require accept() serialization, some don't.. */
          pthread_mutex_lock(&amp;accept_mutex);
          rc = FCGX_Accept_r(&amp;request);
          pthread_mutex_unlock(&amp;accept_mutex);
  
          if (rc &lt; 0)
              break;
  
          server_name = FCGX_GetParam("SERVER_NAME", request.envp);
  
          FCGX_FPrintF(request.out,…
          …     
  
          FCGX_Finish_r(&amp;request);
      }
  
      return NULL;
  }
  
  int main(void)
  {
      int i;
      pthread_t id[THREAD_COUNT];
  
      FCGX_Init();
  
      for (i = 1; i &lt; THREAD_COUNT; i++)
          pthread_create(&amp;id[i], NULL, doit, (void*)i);
  
      doit(0);
  
      return 0;
  }
  </code></pre>
  
  <p>In the <a href="http://www.fastcgi.com/drupal/node/6?q=node/22#S4" rel="noreferrer">FastCGI specification</a> there is an explaination, how the web server will determine how many connections are supported by the FastCGI app:</p>
  
  <blockquote>
    <p>The Web server can query specific
    variables within the application. The
    server will typically perform a query
    on application startup in order to to
    automate certain aspects of system
    configuration.</p>
    
    <p>…</p>
    
    <p>•  FCGI_MAX_CONNS: The maximum number
    of concurrent transport connections
    this application will accept, e.g. "1"
    or "10". </p>
    
    <p>•  FCGI_MAX_REQS: The maximum number
    of concurrent requests this
    application will accept, e.g. "1" or
    "50". </p>
    
    <p>•  FCGI_MPXS_CONNS: "0" if this
    application does not multiplex
    connections (i.e. handle concurrent
    requests over each connection), "1"
    otherwise.</p>
  </blockquote>
  
  <p>But the return values for this query are hard coded into the FastCGI SDK and returns 1 for FCGI_MAX_CONNS and FCGI_MAX_REQS and 0 for FCGI_MPXS_CONNS. So the threaded.c sample will never receive multiple connections.</p>
  
  <p>I tested the sample with lighttpd and nginx and the app handled only one request at once. How can I get my application to handle multiple requests? Or is this the wrong approach?</p>