-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathlock-across-threads.php
More file actions
executable file
·56 lines (48 loc) · 1.69 KB
/
lock-across-threads.php
File metadata and controls
executable file
·56 lines (48 loc) · 1.69 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
<?php
declare(strict_types=1);
/**
* This example shows how to use locks across threads.
*
* Once executed, it prints out "12345678". The numbers printed out are to show the order of the code execution.
*
* Note that this example works only on Swoole 6.0.0 or later, with ZTS (Zend Thread Safety) enabled.
*
* How to run this script:
* docker run --rm -v "$(pwd):/var/www" -ti phpswoole/swoole:6.0-zts php ./examples/locks/lock-across-threads.php
*/
use Swoole\Thread;
use Swoole\Thread\Lock;
if (version_compare(SWOOLE_VERSION, '6.0.0', '<')) {
fwrite(STDERR, 'Error: Swoole 6.0.0 or higher is required. Current version: ' . SWOOLE_VERSION . PHP_EOL);
exit(1);
}
$args = Thread::getArguments();
if (!isset($args)) { // The main thread.
$lock = new Lock();
$threads = [];
$threads[] = new Thread(__FILE__, 1, $lock);
$threads[] = new Thread(__FILE__, 2, $lock);
foreach ($threads as $thread) {
$thread->join();
}
echo '8', PHP_EOL;
} else {
$i = $args[0];
$lock = $args[1];
if ($i === 1) { // First child thread.
echo '1';
assert($lock->lock() === true, 'Lock the lock for the first time successfully.');
usleep(5000); // Sleep for 5 milliseconds.
echo '4';
assert($lock->unlock() === true, 'Unlock the lock successfully.');
echo '5';
} else { // Second child thread.
echo '2';
assert($lock->trylock() === false, 'Failed to lock a locked lock.');
echo '3';
assert($lock->lock() === true, 'Lock the lock for the second time successfully.');
echo '6';
assert($lock->unlock() === true, 'Unlock the lock successfully.');
echo '7';
}
}