File: threaded.pl

package info (click to toggle)
libfcgi-perl 0.79%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 148 kB
  • sloc: perl: 149; makefile: 10
file content (48 lines) | stat: -rw-r--r-- 1,149 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/perl
use strict;
use warnings;
use threads;
use threads::shared;

use FCGI       qw[];
use IO::Handle qw[];

use constant THREAD_COUNT => 5;

my @count : shared = (0, (0) x THREAD_COUNT);

sub worker {
    my $k = shift;
    my %env;
    my $in  = IO::Handle->new;
    my $out = IO::Handle->new;
    my $err = IO::Handle->new;

    my $request = FCGI::Request($in, $out, $err, \%env);

    while ($request->Accept >= 0) {
        print $out
               "Content-type: text/html\r\n",
               "\r\n",
               "<title>FastCGI Hello! (multi-threaded perl, fcgiapp library)</title>",
               "<h1>FastCGI Hello! (multi-threaded perl, fcgiapp library)</h1>",
               "Request counts for ", THREAD_COUNT ," threads ",
               "running on host <i>$env{SERVER_NAME}</i>";

        {
            lock(@count);

            ++$count[$k];

            for(my $i = 1; $i <= THREAD_COUNT; $i++) {
                print $out $count[$i];
                print $out " ";
            }
        }
        $request->Flush;
        sleep(1);
    }
}

$_->join for map { threads->create(\&worker, $_) } 1..THREAD_COUNT;