-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmpmt1.pl
More file actions
60 lines (55 loc) · 1.17 KB
/
mpmt1.pl
File metadata and controls
60 lines (55 loc) · 1.17 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env perl
#
# mpmt1.pl: Perl version of mpmt1.py
#
# License:
# Apache License, Version 2.0
# History:
# * 2022/04/16 v0.1 Initial version
# Author:
# Masanori Itoh <masanori.itoh@gmail.com>
# TODO:
# * Implement multi process model
use strict;
use Getopt::Std;
use Time::HiRes;
use threads;
my $busy_worker = sub
{
my $idx = shift(@_);
my $duration = shift(@_);
printf "busy_worker: %d\n", $idx;
my $ts_start;
my $ts_now;
my $continue = 1;
$ts_start = Time::HiRes::gettimeofday;
while ($continue) {
$ts_now = Time::HiRes::gettimeofday;
if (($ts_now - $ts_start) > $duration) {
printf "busy_worker: exiting... %s\n", $ts_now - $ts_start;
$continue = 0;
}
}
};
#
# main routine
#
my @th;
my $num_context = 2;
my $duration = 5.0;
my $mode = 'thread';
my %opt;
getopts('n:d:m:', \%opt);
if (exists $opt{n}) {
$num_context = $opt{n};
}
if (exists $opt{d}) {
$duration = $opt{d};
}
printf "mpmt1.pl: num_context = %d duration = %f\n", $num_context, $duration;
for (my $i = 0; $i < $num_context; $i++) {
$th[$i] = threads->new(\&$busy_worker, $i, $duration)
}
for (my $i = 0; $i < $num_context; $i++) {
$th[$i]->join;
}