2 * threaded.c -- A simple multi-threaded FastCGI application.
6 static const char rcsid[] = "$Id: threaded.c,v 1.9 2001/11/20 03:23:21 robs Exp $";
9 #include "fcgi_config.h"
12 #include <sys/types.h>
21 #define THREAD_COUNT 20
23 static int counts[THREAD_COUNT];
25 static void *doit(void *a)
27 int rc, i, thread_id = (int)a;
32 FCGX_InitRequest(&request, 0, 0);
36 static pthread_mutex_t accept_mutex = PTHREAD_MUTEX_INITIALIZER;
37 static pthread_mutex_t counts_mutex = PTHREAD_MUTEX_INITIALIZER;
39 /* Some platforms require accept() serialization, some don't.. */
40 pthread_mutex_lock(&accept_mutex);
41 rc = FCGX_Accept_r(&request);
42 pthread_mutex_unlock(&accept_mutex);
47 server_name = FCGX_GetParam("SERVER_NAME", request.envp);
49 FCGX_FPrintF(request.out,
50 "Content-type: text/html\r\n"
52 "<title>FastCGI Hello! (multi-threaded C, fcgiapp library)</title>"
53 "<h1>FastCGI Hello! (multi-threaded C, fcgiapp library)</h1>"
54 "Thread %d, Process %ld<p>"
55 "Request counts for %d threads running on host <i>%s</i><p><code>",
56 thread_id, pid, THREAD_COUNT, server_name ? server_name : "?");
60 pthread_mutex_lock(&counts_mutex);
62 for (i = 0; i < THREAD_COUNT; i++)
63 FCGX_FPrintF(request.out, "%5d " , counts[i]);
64 pthread_mutex_unlock(&counts_mutex);
66 FCGX_Finish_r(&request);
75 pthread_t id[THREAD_COUNT];
79 for (i = 1; i < THREAD_COUNT; i++)
80 pthread_create(&id[i], NULL, doit, (void*)i);