Version 0.73
[catagits/fcgi2.git] / perl / threaded.PL
1 use Config;
2
3 open OUT, ">threaded.fpl";
4 print OUT "#!$Config{perlpath}\n";
5 print OUT while <DATA>;
6 close OUT;
7 chmod 0755, "threaded.fpl";
8 __END__
9
10 use FCGI;
11 use Thread;
12 use IO::Handle;
13
14 use constant THREAD_COUNT => 5;
15
16 sub doit {
17     my $k = shift;
18     my %env;
19     my $in = new IO::Handle;
20     my $out = new IO::Handle;
21     my $err = new IO::Handle;
22
23     my $request = FCGI::Request($in, $out, $err, \%env);
24
25     while ($request->Accept() >= 0) {
26         print $out
27            "Content-type: text/html\r\n",
28            "\r\n",
29            "<title>FastCGI Hello! (multi-threaded perl, fcgiapp library)</title>",
30            "<h1>FastCGI Hello! (multi-threaded perl, fcgiapp library)</h1>",
31            "Request counts for ", THREAD_COUNT ," threads ",
32            "running on host <i>$env{SERVER_NAME}</i><P><CODE>";
33
34         {
35             lock(@count);
36
37             ++$count[$k];
38
39             for(my $i = 0; $i < THREAD_COUNT; ++$i) {
40                 print $out $count[$i];
41                 print $out " ";
42             }
43         }
44         $request->Flush();
45         sleep(1);
46     }
47 }
48
49 for ($t = 1; $t < THREAD_COUNT; ++$t) {
50     new Thread \&doit, $t;
51 }
52 doit(0);