*) Add multithread support to the fcgiapp lib and an example multithreaded
[catagits/fcgi2.git] / examples / threaded.c
1 /* 
2  * threaded.c -- A simple multi-threaded FastCGI application.
3  */
4
5 #ifndef lint
6 static const char rcsid[] = "$Id: threaded.c,v 1.1 1999/07/26 04:28:07 roberts Exp $";
7 #endif /* not lint */
8
9 #if defined HAVE_UNISTD_H || defined __linux__
10 #include <unistd.h>
11 #endif
12
13 #include "fcgiapp.h"
14
15 #ifdef _WIN32
16 #include <process.h>
17 #endif
18
19 #include <pthread.h>
20
21 #define THREAD_COUNT 20
22
23 int count[THREAD_COUNT];
24
25 static void *doit(void *a)
26 {
27     int k = (int)a;
28     FCGX_Request request;
29     FCGX_Stream *in, *out, *err;
30     FCGX_ParamArray envp;
31     int i;
32
33     FCGX_InitRequest(&request);
34
35     while (FCGX_Accept_r(&in, &out, &err, &envp, &request) >= 0)
36     {
37         FCGX_FPrintF(out,
38            "Content-type: text/html\r\n"
39            "\r\n"
40            "<title>FastCGI Hello! (multi-threaded C, fcgiapp library)</title>"
41            "<h1>FastCGI Hello! (multi-threaded C, fcgiapp library)</h1>"
42            "Request counts for %d threads running on host <i>%s</i><P><CODE>",
43                THREAD_COUNT, FCGX_GetParam("SERVER_NAME", envp));
44
45         count[k]++;
46
47         for (i = 0; i < THREAD_COUNT; i++)
48             FCGX_FPrintF(out, "%5d " , count[i]);
49     }
50
51     return NULL;
52 }
53
54 int main(void)
55 {
56     int i;
57     pthread_t id[THREAD_COUNT];
58
59     FCGX_Init();
60
61     for (i = 0; i < THREAD_COUNT; i++)
62         count[i] = 0;
63
64     for (i = 1; i < THREAD_COUNT; i++)
65         pthread_create(&id[i], NULL, doit, (void*)i);
66    
67     doit(0);
68
69     exit(0);
70 }
71