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