-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThreadPHP.php
More file actions
58 lines (46 loc) · 1.33 KB
/
Copy pathThreadPHP.php
File metadata and controls
58 lines (46 loc) · 1.33 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
<?php
require('Prime.php');
if (!class_exists('Thread')) {
print("Please enable pthreads extension. More info: https://www.php.net/manual/en/pthreads.installation.php");
exit(1);
}
interface Runnable { public function run();}
class RunnableThread extends Thread implements Runnable {
private $num;
private $startAt;
public function __construct(int $num) {
$this->num = $num;
$this->startAt = microtime(true);
}
public function run() {
$prime = new Prime($this->num);
$count = $prime->count();
$endAt = (microtime(true) - $this->startAt) * 1000;
printf("PHP thread id #%d: executes %d prime numbers in %d ms\n",
Thread::getCurrentThreadId(),
$count, $endAt);
}
}
function main(array $args) {
if (php_sapi_name() !='cli' || !isset($args[1])) {
print("Invalid argument".PHP_EOL);
exit(1);
}
$num = intval($args[1]);
print("PHP Threads - initializing complete\n");
$t0 = new RunnableThread($num);
$t1 = new RunnableThread($num);
$t2 = new RunnableThread($num);
$t3 = new RunnableThread($num);
$t0->start();
$t1->start();
$t2->start();
$t3->start();
try {
$t0->join();
$t1->join();
$t2->join();
$t3->join();
} catch (Exception $e) {}
}
main($argv);