forked from birdwyx/phpgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselector_select.php
52 lines (44 loc) · 1.04 KB
/
selector_select.php
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
<?php
use \Go\Chan;
use \Go\Time;
use \Go\Scheduler;
$ch1 = new Chan(["capacity"=>1]);
$ch2 = new Chan(["capacity"=>1]);
$exit = new Chan(0);
go(function() use($ch1, $ch2, $exit){
$selector = select(
[
'case', $ch1, function($value){
echo "go 0: receive from ch1: " . $value . PHP_EOL;
}
],
[
'case', $ch2, function($value){
echo "go 0: receive from ch2: " . $value . PHP_EOL;
}
]
);
echo "go 0: after 1st select\n";
while( $exit->tryPop() !== NULL ) $selector->select();
});
go(function() use($ch1, $exit){
$i = 0;
while( $exit->tryPop() !== NULL ){ //===NULL: channel closed
echo "go 1: push $i\n";
$ch1->push($i++);
}
});
go(function() use($ch2, $exit){
$i = 0;
while( $exit->tryPop() !== NULL ){
echo "go 2: push $i\n";
$ch2->push($i++);
}
});
go(function() use($exit){
echo "main: now sleep 5ms\n";
Time::sleep(5*1000*1000);
echo "main: now close the exit channel\n";
$exit->close();
});
Scheduler::join();