PHP 8.1.0 Released!

pht\AtomicInteger::lock

(PECL pht >= 0.0.1)

pht\AtomicInteger::lockAcquires the atomic integer's mutex lock

Description

public pht\AtomicInteger::lock(): void

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.

Parameters

This function has no parameters.

Return Values

No return value.

Examples

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());

The above example will output:

int(4)

add a note add a note

User Contributed Notes 1 note

up
0
Anonymous
21 days ago
Just a note that AtomicInteger should not expose Lock, as this is an implementation detail of atomicity.

https://www.internalpointers.com/post/lock-free-multithreading-atomic-operations

While not all architectures have lock-free primitive support, the fact some do, mean that this exposure is a second interface atop Atomicity of an Integer

    final class AtomicLockingInteger extends AtomicInteger implements Lockable {
        /** ... */
    }
To Top