PHP - Manual: pht\AtomicInteger::lock
2024-12-27
(PECL pht >= 0.0.1)
pht\AtomicInteger::lock — Acquires the atomic integer's mutex lock
This method will acquire the mutex lock associated with the atomic integer. The mutex lock only needs to be acquired when needing to group together multiple operations.
The mutex locks of the atomic values are reentrant safe. It is therefore valid for the same thread to reacquire a mutex lock that it has already acquired.
此函数没有参数。
No return value.
Example #1 Grouping together an atomic integer's operations (requiring a mutex lock)
<?php
use pht\AtomicInteger;
$atomicInteger = new AtomicInteger(2);
// assumes $atomicInteger is being used by multiple threads
$atomicInteger->lock();
$atomicInteger->set($atomicInteger->get() * 2); // double the value
$atomicInteger->unlock();
var_dump($atomicInteger->get());
以上例程会输出:
int(4)
官方地址:https://www.php.net/manual/en/pht-atomicinteger.lock.php