GoLang pht\Queue::lock

request it (395)
GoLang replacement for PHP's pht\Queue::lock [edit | history]



Do you know a GoLang replacement for PHP's pht\Queue::lock? Write it!

PHP pht\Queue::lock

PHP original manual for pht\Queue::lock [ show | php.net ]

pht\Queue::lock

(PECL pht >= 0.0.1)

pht\Queue::lockAcquires the queue's mutex lock

Description

public void pht\Queue::lock ( void )

This method will acquire the mutex lock associated with the queue. The mutex lock should always be acquired when manipulating the queue if it is being used by multiple threads.

The mutex locks of the Inter-Thread Communication (ITC) data structures are not reentrant. Attempting to reacquire an already-acquired mutex lock by the same thread will therefore cause a deadlock.

Parameters

This function has no parameters.

Return Values

No return value.

Examples

Example #1 Locking a queue's mutex lock

<?php

use pht\{ThreadQueue};

$thread = new Thread();
$queue = new Queue();

$thread->addFunctionTask(function ($queue) {
    
$queue->lock();
    
$queue->push(1);
    
$queue->unlock();
}, 
$queue);

$thread->start();

// $queue is currently being used by multiple threads
$queue->lock();
$queue->push(1);
$queue->unlock();

$thread->join();

// $queue is only being used in this thread now, so no need to lock it
while ($queue->size()) {
    
var_dump($queue->pop());
}

The above example will output:

int(1)
int(1)