Book/pht-Phpdoc专题
pht
目录
- 简介
- 安装/配置
- pht\Thread — The Thread class
- pht\Thread::addClassTask — Class threading
- pht\Thread::addFileTask — File threading
- pht\Thread::addFunctionTask — Function threading
- pht\Thread::join — Joins a thread
- pht\Thread::start — Starts the new thread
- pht\Thread::taskCount — Gets a thread's task count
- pht\Runnable — The Runnable interface
- pht\Runnable::run — The entry point of a threaded class
- pht\HashTable — The HashTable class
- pht\HashTable::lock — Acquires the hash table's mutex lock
- pht\HashTable::size — Gets the size of the hash table
- pht\HashTable::unlock — Releases the hash table's mutex lock
- pht\Vector — The Vector class
- pht\Vector::__construct — Vector creation
- pht\Vector::deleteAt — Deletes a value in the vector
- pht\Vector::insertAt — Inserts a value into the vector
- pht\Vector::lock — Acquires the vector's mutex lock
- pht\Vector::pop — Pops a value to the vector
- pht\Vector::push — Pushes a value to the vector
- pht\Vector::resize — Resizes a vector
- pht\Vector::shift — Shifts a value from the vector
- pht\Vector::size — Gets the size of the vector
- pht\Vector::unlock — Releases the vector's mutex lock
- pht\Vector::unshift — Unshifts a value to the vector front
- pht\Vector::updateAt — Updates a value in the vector
- pht\Queue — The Queue class
- pht\Queue::front — Returns the first value from a queue
- pht\Queue::lock — Acquires the queue's mutex lock
- pht\Queue::pop — Pops a value off of the front of a queue
- pht\Queue::push — Pushes a value to the end of a queue
- pht\Queue::size — Gets the size of the queue
- pht\Queue::unlock — Releases the queue's mutex lock
- pht\AtomicInteger — The
AtomicInteger class
- pht\AtomicInteger::__construct — AtomicInteger creation
- pht\AtomicInteger::dec — Decrements the atomic integer's value by one
- pht\AtomicInteger::get — Gets the atomic integer's value
- pht\AtomicInteger::inc — Increments the atomic integer's value by one
- pht\AtomicInteger::lock — Acquires the atomic integer's mutex lock
- pht\AtomicInteger::set — Sets the atomic integer's value
- pht\AtomicInteger::unlock — Releases the atomic integer's mutex lock
- pht\Threaded — The Threaded interface
- pht\Threaded::lock — Acquires the mutex lock
- pht\Threaded::unlock — Releases the mutex lock
简介
The pht\Thread class abstracts away a native thread. It has an internal task queue, where the methods <span class="methodname">pht\Thread::addClassTask, <span class="methodname">pht\Thread::addFunctionTask, and <span class="methodname">pht\Thread::addFileTask push new tasks onto this queue. Invoking the <span class="methodname">pht\Thread::start method will cause the new thread to be spawned, where it will then begin working through the task queue. A thread may be reused for any number of tasks.
类摘要
pht\Thread
class pht\Thread {
/* 方法 */
public void
addClassTask ( <span
class="methodparam">string $className
, mixed
$ctorArgs )
public void
addFileTask ( <span
class="methodparam">string $fileName
, mixed
$globals )
public void
addFunctionTask ( <span
class="methodparam">callable $func ,
mixed
$funcArgs )
public void join ( <span class="methodparam">void )
public void start ( <span class="methodparam">void )
public int <span class="methodname">taskCount ( <span class="methodparam">void )
}
pht\Thread::addClassTask
Class threading
说明
public void
pht\Thread::addClassTask ( <span
class="methodparam">string $className
, mixed
$ctorArgs )
Adds a new class task to a pht\Threads internal task queue.
参数
className
The name of the class to be threaded. This class must implement the
pht\Runnable interface.
ctorArgs
An optional list of arguments for the threaded class' constructor. These
arguments will be serialised (since they are being passed to another
thread).
返回值
No return value.
范例
示例 #1 Adding a new class task to a thread
<?php
use pht\{Thread, Runnable};
class Task implements Runnable
{
private $one;
public function __construct(int $one)
{
$this->one = $one;
}
public function run()
{
var_dump($this->one);
}
}
$thread = new Thread();
$thread->addClassTask(Task::class, 1);
$thread->start();
$thread->join();
以上例程会输出:
int(1)
pht\Thread::addFileTask
File threading
说明
public void
pht\Thread::addFileTask ( <span
class="methodparam">string $fileName
, mixed
$globals )
Adds a new file task to a pht\Threads internal task queue.
参数
func
The name of the file to be threaded.
globals
An optional list of arguments for the file. These arguments will be
placed into a $_THREAD superglobal, which will be made available
inside of the threaded file. All arguments will be serialised (since
they are being passed to another thread).
返回值
No return value.
范例
示例 #1 Adding a new file task to a thread
<?php
use pht\Thread;
$thread = new Thread();
$thread->addFileTask('file.php', 1, 2, 3);
$thread->start();
$thread->join();
file.php:
<?php
[$one, $two, $three] = $_THREAD;
var_dump($one, $two, $three);
以上例程会输出:
int(1)
int(2)
int(3)
pht\Thread::addFunctionTask
Function threading
说明
public void
pht\Thread::addFunctionTask ( <span
class="methodparam">callable $func ,
mixed
$funcArgs )
Adds a new function task to a <span class="classname">pht\Threads internal task queue.
参数
func
The function to be threaded. If it is bound to an instance, then $this
will become null.
funcArgs
An optional list of arguments for the function. These arguments will be
serialised (since they are being passed to another thread).
返回值
No return value.
范例
示例 #1 Adding a new function task to a thread
<?php
use pht\Thread;
class Test
{
public static function run(){var_dump(5);}
public static function run2(){var_dump(6);}
}
function aFunc(){var_dump(3);}
$thread = new Thread();
$thread->addFunctionTask(static function($one) {var_dump($one);}, 1);
$thread->addFunctionTask(function() {var_dump(2);});
$thread->addFunctionTask('aFunc');
$thread->addFunctionTask('array_map', function ($n) {var_dump($n);}, [4]);
$thread->addFunctionTask(['Test', 'run']);
$thread->addFunctionTask([new Test, 'run2']);
$thread->start();
$thread->join();
以上例程会输出:
int(1)
int(2)
int(3)
int(4)
int(5)
int(6)
pht\Thread::join
Joins a thread
说明
public void pht\Thread::join ( <span class="methodparam">void )
This method will join the spawned thread (though it will first wait for that thread's internal task queue to finish). As a matter of good practice, threads should always be joined. Not joining a thread may lead to undefined behaviour.
参数
此函数没有参数。
返回值
No return value.
范例
示例 #1 Joining a thread
<?php
use pht\Thread;
$thread = new Thread();
$thread->start();
$thread->join();
pht\Thread::start
Starts the new thread
说明
public void pht\Thread::start ( <span class="methodparam">void )
This will cause a new thread to be spawned for the associated <span class="classname">pht\Thread object, where its internal task queue will begin to be processed.
参数
此函数没有参数。
返回值
No return value.
范例
示例 #1 Starting a new thread
<?php
use pht\Thread;
$thread = new Thread();
$thread->start();
$thread->join();
pht\Thread::taskCount
Gets a thread's task count
说明
public int <span class="methodname">pht\Thread::taskCount ( <span class="methodparam">void )
Retrieves the current task count of a <span class="classname">pht\Thread.
参数
此函数没有参数。
返回值
The number of tasks remaining to be processed.
范例
示例 #1 Getting the task count of a thread
<?php
use pht\Thread;
$thread = new Thread();
$thread->addFunctionTask(function (){});
$thread->addFunctionTask(function (){});
$thread->addFunctionTask(function (){});
var_dump($thread->taskCount());
以上例程会输出:
int(3)
简介
The pht\Runnable interface enforces the implementation of a run() method on classes that should be threaded. This method acts as the entry point of the threaded class.
接口摘要
pht\Runnable
class pht\Runnable {
/* 方法 */
public void run ( <span class="methodparam">void )
}
pht\Runnable::run
The entry point of a threaded class
说明
public void pht\Runnable::run ( <span class="methodparam">void )
This method acts as the entry point of execution for a threaded class. It must be defined by all classes that will be threaded.
参数
此函数没有参数。
返回值
No return value.
简介
The pht\HashTable class is one of the Inter-Thread Communication (ITC) data structures exposed by pht. It can be safely passed around between threads, and manipulated by multiple threads using the mutex locks that have been packed in with the data structure. It is reference-counted across threads, and so it does not need to be explicitly destroyed.
The pht\HashTable class enables for array access upon its objects (along with the <span class="function">isset and unset functions). The ArrayAccess interface is not explicitly implemented, however, because it is only needed for such abilities by userland classes.
类摘要
pht\HashTable
class pht\HashTable <span class="oointerface">implements <span class="interfacename">pht\Threaded {
/* 方法 */
public void lock ( <span class="methodparam">void )
public int <span class="methodname">size ( void )
public void unlock ( <span class="methodparam">void )
}
pht\HashTable::lock
Acquires the hash table's mutex lock
说明
public void pht\HashTable::lock ( <span class="methodparam">void )
This method will acquire the mutex lock associated with the hash table. The mutex lock should always be acquired when manipulating the hash table 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.
参数
此函数没有参数。
返回值
No return value.
范例
示例 #1 Locking a hash table's mutex lock
<?php
use pht\{Thread, HashTable};
$thread = new Thread();
$hashTable = new HashTable();
$thread->addFunctionTask(function ($hashTable) {
$hashTable->lock();
$hashTable['a'] = 1;
$hashTable->unlock();
}, $hashTable);
$thread->start();
// $hashTable is currently being used by multiple threads
$hashTable->lock();
$hashTable['b'] = 2;
$hashTable->unlock();
$thread->join();
pht\HashTable::size
Gets the size of the hash table
说明
public int <span class="methodname">pht\HashTable::size ( <span class="methodparam">void )
Returns the current size of the hash table. This operation requires a pht\HashTable's mutex lock to be held if it is being used by multiple threads.
参数
此函数没有参数。
返回值
The size of the hash table.
范例
示例 #1 Getting a hash table's size
<?php
use pht\HashTable;
$hashTable = new HashTable();
$hashTable['a'] = 1;
$hashTable['b'] = 2;
var_dump($hashTable->size());
以上例程会输出:
int(2)
pht\HashTable::unlock
Releases the hash table's mutex lock
说明
public void pht\HashTable::unlock ( <span class="methodparam">void )
This method will release the mutex lock associated with the hash table.
参数
此函数没有参数。
返回值
No return value.
范例
示例 #1 Locking a hash table's mutex lock
<?php
use pht\{Thread, HashTable};
$thread = new Thread();
$hashTable = new HashTable();
$thread->addFunctionTask(function ($hashTable) {
$hashTable->lock();
$hashTable['a'] = 1;
$hashTable->unlock();
}, $hashTable);
$thread->start();
// $hashTable is currently being used by multiple threads
$hashTable->lock();
$hashTable['b'] = 2;
$hashTable->unlock();
$thread->join();
简介
The pht\Vector class is one of the Inter-Thread Communication (ITC) data structures exposed by pht. It can be safely passed around between threads, and manipulated by multiple threads using the mutex locks that have been packed in with the data structure. It is reference-counted across threads, and so is does not need to be explicitly destroyed.
The pht\Vector class enables for array access upon its objects (along with the <span class="function">isset and unset functions). The ArrayAccess interface is not explicitly implemented, however, because it is only needed for such abilities by userland classes.
类摘要
pht\Vector
class pht\Vector <span class="oointerface">implements <span class="interfacename">pht\Threaded {
/* 方法 */
public Vector
__construct ([ <span
class="methodparam">int $size<span
class="initializer"> = 0 [, <span
class="methodparam">mixed $value<span
class="initializer"> = 0 ]] )
public void
deleteAt ( <span
class="methodparam">int $offset )
public void
insertAt ( <span
class="methodparam">mixed $value ,
int $offset
)
public void lock ( <span class="methodparam">void )
public mixed pop ( <span class="methodparam">void )
public void
push ( <span
class="type">mixed $value )
public void
resize ( <span
class="type">int $size [, <span
class="methodparam">mixed $value<span
class="initializer"> = 0 ] )
public mixed shift ( <span class="methodparam">void )
public int <span class="methodname">size ( void )
public void unlock ( <span class="methodparam">void )
public void
unshift ( <span
class="methodparam">mixed $value )
public void
updateAt ( <span
class="methodparam">mixed $value ,
int $offset
)
}
pht\Vector::__construct
Vector creation
说明
public Vector
pht\Vector::__construct ([ <span
class="methodparam">int $size<span
class="initializer"> = 0 [, <span
class="methodparam">mixed $value<span
class="initializer"> = 0 ]] )
Handles the creation of a new vector.
参数
size
The size of the vector that will be created.
value
The value to initialise the empty slots in the vector to.
返回值
No return value.
范例
示例 #1 Creating a new vector
<?php
use pht\Vector;
$vector1 = new Vector(1);
$vector2 = new Vector(2, 1);
var_dump($vector1, $vector2);
以上例程会输出:
object(pht\Vector)#1 (1) {
[0]=>
int(0)
}
object(pht\Vector)#2 (2) {
[0]=>
int(1)
[1]=>
int(1)
}
pht\Vector::deleteAt
Deletes a value in the vector
说明
public void
pht\Vector::deleteAt ( <span
class="methodparam">int $offset )
This method deletes a value at the specified offset in the vector (in linear time).
Since the pht\Vector class supports array access, deleting values can also be performed using the array subset notation ([]) in combination with the <span class="function">unset function.
参数
offset
The offset at which the value will be deleted at. This offset must be
within the 0..(N-1) range (inclusive), where N is the size of the
vector. Attempting to delete at offsets outside of this range will
result in an Error exception.
返回值
No return value.
范例
示例 #1 Deleting values in a vector
<?php
use pht\Vector;
$vector = new Vector();
$vector[] = 1;
$vector[] = 2;
$vector[] = 3;
$vector[] = 4;
$vector->deleteAt(1);
unset($vector[1]);
var_dump($vector);
以上例程会输出:
object(pht\Vector)#1 (2) {
[0]=>
int(1)
[1]=>
int(4)
}
pht\Vector::insertAt
Inserts a value into the vector
说明
public void
pht\Vector::insertAt ( <span
class="methodparam">mixed $value ,
int $offset
)
This method inserts a value at the specified offset into the vector (in linear time). The vector will automatically be resized if it is not large enough.
参数
value
The value to be inserted into the vector. This value will be serialised
(since it may be passed around between threads).
offset
The offset at which the value will be inserted at. This offset must be
within the 0..N range (inclusive), where N is the size of the vector.
Inserting at position N is the equivalent of using <span
class="methodname">pht\Vector::push, and inserting at position 0
is the equivalent of using <span
class="methodname">pht\Vector::unshift. Attempting to insert at
offsets outside of this range will result in an <span
class="classname">Error exception.
返回值
No return value.
范例
示例 #1 Inserting a value into a vector
<?php
use pht\Vector;
$vector = new Vector();
$vector->insertAt(3, 0); // insert 3 at start
$vector->insertAt(1, 0); // insert 1 at start (before 3)
$vector->insertAt(2, 1); // insert 2 in middle (after 1 and before 3)
var_dump($vector);
以上例程会输出:
object(pht\Vector)#1 (3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
pht\Vector::lock
Acquires the vector's mutex lock
说明
public void pht\Vector::lock ( <span class="methodparam">void )
This method will acquire the mutex lock associated with the vector. The mutex lock should always be acquired when manipulating the vector 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.
参数
此函数没有参数。
返回值
No return value.
范例
示例 #1 Locking a vector's mutex lock
<?php
use pht\{Thread, Vector};
$thread = new Thread();
$vector = new Vector();
$thread->addFunctionTask(function ($vector) {
$vector->lock();
$vector[] = 1;
$vector->unlock();
}, $vector);
$thread->start();
// $vector is currently being used by multiple threads
$vector->lock();
$vector[] = 2;
$vector->unlock();
$thread->join();
pht\Vector::pop
Pops a value to the vector
说明
public mixed pht\Vector::pop ( <span class="methodparam">void )
This method pops a value from the end of a vector (in constant time). Popping a value from an empty vector will result in an <span class="classname">Error exception.
参数
此函数没有参数。
返回值
The value from the end of the vector.
范例
示例 #1 Popping a value from a vector
<?php
use pht\Vector;
$vector = new Vector();
$vector->push(1);
$vector[] = 2;
var_dump($vector->pop());
以上例程会输出:
int(2)
pht\Vector::push
Pushes a value to the vector
说明
public void
pht\Vector::push ( <span
class="methodparam">mixed $value )
This method pushes a value onto the end of a vector (in constant time). The vector will automatically be resized if it is not large enough.
Since the pht\Vector class supports array access, new values can also be pushed onto the vector using the empty subset notation ([]).
参数
value
The value to be pushed onto the end of the vector. This value will be
serialised (since it may be passed around between threads).
返回值
No return value.
范例
示例 #1 Pushing values to a vector
<?php
use pht\Vector;
$vector = new Vector();
$vector->push(1);
$vector[] = 2;
var_dump($vector);
以上例程会输出:
object(pht\Vector)#1 (2) {
[0]=>
int(1)
[1]=>
int(2)
}
pht\Vector::resize
Resizes a vector
说明
public void
pht\Vector::resize ( <span
class="methodparam">int $size [,
mixed $value<span
class="initializer"> = 0 ] )
Resizes the vector. If it is enlarged, then the value parameter will
be used to fill in the new slots. If it is made smaller, then the end
values will be truncated.
参数
size
The new size of the vector.
value
The value to initialise the empty vector slots to (only used if the
vector is enlarged).
返回值
No return value.
范例
示例 #1 Resizing a vector
<?php
use pht\Vector;
$vector = new Vector(1);
var_dump($vector);
$vector->resize(2, 1);
var_dump($vector);
$vector->resize(1, 2); // unused second arg
var_dump($vector);
以上例程会输出:
object(pht\Vector)#1 (1) {
[0]=>
int(0)
}
object(pht\Vector)#1 (2) {
[0]=>
int(0)
[1]=>
int(1)
}
object(pht\Vector)#1 (1) {
[0]=>
int(0)
}
pht\Vector::shift
Shifts a value from the vector
说明
public mixed pht\Vector::shift ( <span class="methodparam">void )
This method shifts a value from the front of a vector (in linear time). Shifting a value from an empty vector will result in an <span class="classname">Error exception.
参数
此函数没有参数。
返回值
The value from the front of the vector.
范例
示例 #1 Shifting a value from a vector
<?php
use pht\Vector;
$vector = new Vector();
$vector->push(1);
$vector[] = 2;
var_dump($vector->shift());
以上例程会输出:
int(1)
pht\Vector::size
Gets the size of the vector
说明
public int <span class="methodname">pht\Vector::size ( <span class="methodparam">void )
Returns the current size of the vector. This operation requires a <span class="classname">pht\Vector's mutex lock to be held if it is being used by multiple threads.
参数
此函数没有参数。
返回值
The size of the vector.
范例
示例 #1 Getting a vector's size
<?php
use pht\Vector;
$vector = new Vector();
$vector[] = 1;
$vector[] = 2;
$vector[] = 3;
var_dump($vector->size());
以上例程会输出:
int(3)
pht\Vector::unlock
Releases the vector's mutex lock
说明
public void pht\Vector::unlock ( <span class="methodparam">void )
This method will release the mutex lock associated with the vector.
参数
此函数没有参数。
返回值
No return value.
范例
示例 #1 Locking a vector's mutex lock
<?php
use pht\{Thread, Vector};
$thread = new Thread();
$vector = new Vector();
$thread->addFunctionTask(function ($vector) {
$vector->lock();
$vector[] = 1;
$vector->unlock();
}, $vector);
$thread->start();
// $vector is currently being used by multiple threads
$vector->lock();
$vector[] = 2;
$vector->unlock();
$thread->join();
pht\Vector::unshift
Unshifts a value to the vector front
说明
public void
pht\Vector::unshift ( <span
class="methodparam">mixed $value )
This method unshifts a value to the front of a vector (in linear time). The vector will automatically be resized if it is not large enough.
参数
value
The value to be pushed onto the beginning of the vector. This value will
be serialised (since it may be passed around between threads).
返回值
No return value.
范例
示例 #1 Unshifting a value to the front of a vector
<?php
use pht\Vector;
$vector = new Vector();
for ($i = 0; $i < 3; ++$i) {
$vector->unshift($i); // causes a quadratic runtime, beware
}
var_dump($vector);
以上例程会输出:
object(pht\Vector)#1 (3) {
[0]=>
int(2)
[1]=>
int(1)
[2]=>
int(0)
}
pht\Vector::updateAt
Updates a value in the vector
说明
public void
pht\Vector::updateAt ( <span
class="methodparam">mixed $value ,
int $offset
)
This method updates a value at the specified offset in the vector (in linear time). The vector will automatically be resized if it is not large enough.
Since the pht\Vector class supports array access, updating values can also be performed using the array subset notation ([]).
参数
value
The value to be inserted into the vector. This value will be serialised
(since it may be passed around between threads).
offset
The offset at which the value will be updated at. This offset must be
within the 0..(N-1) range (inclusive), where N is the size of the
vector. Attempting to update at offsets outside of this range will
result in an Error exception.
返回值
No return value.
范例
示例 #1 Updating a value in a vector
<?php
use pht\Vector;
$vector = new Vector();
$vector[] = 1;
$vector[] = 2;
$vector->updateAt(3, 0);
$vector[1] = 4;
var_dump($vector);
以上例程会输出:
object(pht\Vector)#1 (2) {
[0]=>
int(3)
[1]=>
int(4)
}
简介
The pht\Queue class is one of the Inter-Thread Communication (ITC) data structures exposed by pht. It can be safely passed around between threads, and manipulated by multiple threads using the mutex locks that have been packed in with the data structure. It is reference-counted across threads, and so it does not need to be explicitly destroyed.
类摘要
pht\Queue
class pht\Queue <span class="oointerface">implements <span class="interfacename">pht\Threaded {
/* 方法 */
public mixed front ( <span class="methodparam">void )
public void lock ( <span class="methodparam">void )
public mixed pop ( <span class="methodparam">void )
public void
push ( <span
class="type">mixed $value )
public int <span class="methodname">size ( void )
public void unlock ( <span class="methodparam">void )
}
pht\Queue::front
Returns the first value from a queue
说明
public mixed pht\Queue::front ( <span class="methodparam">void )
This method will remove a value from the front of the queue (in constant time). Attempting to return the front value from an empty queue will result in an Error exception.
Caution
Due to the fact that all values in a <span class="classname">pht\Queue are serialised, extracting a value from the queue will require it to be deserialised. This can incur a noticeable performance hit if the inspection of the queue's front value is performed within a loop.
参数
此函数没有参数。
返回值
The value on the front of the queue.
范例
示例 #1 Retrieving the front value of a queue
<?php
use pht\Queue;
$queue = new Queue();
$queue->push(1);
var_dump($queue->front());
以上例程会输出:
int(1)
示例 #2 Retrieving the front value in a loop (bad example - don't do this)
<?php
use pht\Queue;
$queue = new Queue();
$queue->push(array_fill(0, 2000, 0));
for ($i = 0; $i < count($queue->front()); ++$i); // quadratic runtime
示例 #3 Retrieving the front value in a loop (good example)
<?php
use pht\Queue;
$queue = new Queue();
$queue->push(array_fill(0, 2000, 0));
$front = $queue->front(); // create a separate variable
for ($i = 0; $i < count($front); ++$i); // linear runtime
pht\Queue::lock
Acquires the queue's mutex lock
说明
public void pht\Queue::lock ( <span class="methodparam">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.
参数
此函数没有参数。
返回值
No return value.
范例
示例 #1 Locking a queue's mutex lock
<?php
use pht\{Thread, Queue};
$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());
}
以上例程会输出:
int(1)
int(1)
pht\Queue::pop
Pops a value off of the front of a queue
说明
public mixed pht\Queue::pop ( <span class="methodparam">void )
This method will remove a value from the front of the queue (in constant time). Attempting to pop a value from an empty queue will result in an Error exception.
参数
此函数没有参数。
返回值
The value removed from the queue.
范例
示例 #1 Popping a value from a queue
<?php
use pht\Queue;
$queue = new Queue();
$queue->push(1);
var_dump($queue->pop());
以上例程会输出:
int(1)
pht\Queue::push
Pushes a value to the end of a queue
说明
public void
pht\Queue::push ( <span
class="methodparam">mixed $value )
This method will add a value onto the queue.
参数
value
The value to be added to a pht\Queue.
This value will be serialised (since it may be passed around between
threads).
返回值
No return value.
范例
示例 #1 Pushing a value to a queue
<?php
use pht\Queue;
$queue = new Queue();
$queue->push(1);
var_dump($queue);
以上例程会输出:
object(pht\Queue)#1 (1) {
[0]=>
int(1)
}
pht\Queue::size
Gets the size of the queue
说明
public int <span class="methodname">pht\Queue::size ( <span class="methodparam">void )
Returns the current size of the queue. This operation requires a <span class="classname">pht\Queue's mutex lock to be held if it is being used by multiple threads.
参数
此函数没有参数。
返回值
The size of the queue.
范例
示例 #1 Getting a queue's size
<?php
use pht\Queue;
$queue = new Queue();
$queue->push(1);
$queue->push(1);
var_dump($queue->size());
以上例程会输出:
int(2)
pht\Queue::unlock
Releases the queue's mutex lock
说明
public void pht\Queue::unlock ( <span class="methodparam">void )
This method will release the mutex lock associated with the queue.
参数
此函数没有参数。
返回值
No return value.
范例
示例 #1 Locking a queue's mutex lock
<?php
use pht\{Thread, Queue};
$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());
}
以上例程会输出:
int(1)
int(1)
简介
The pht\AtomicInteger class is currently the only supported atomic value. It allows for an integer to be safely passed around between, and manipulated, by multiple threads. The methods exposed by this class do not need mutex locking, since they will acquire the internal mutex lock implicitly. <span class="methodname">pht\AtomicInteger::lock and <span class="methodname">pht\AtomicInteger::unlock are still exposed, however, for when multiple operations involving the same <span class="classname">pht\AtomicInteger object need to be grouped together.
The mutex locks of the atomic values are reentrant safe.
类摘要
pht\AtomicInteger
class pht\AtomicInteger <span class="oointerface">implements <span class="interfacename">pht\Threaded {
/* 方法 */
public <span
class="type">AtomicInteger <span
class="methodname">__construct ([ <span
class="methodparam">int $value<span
class="initializer"> = 0 ] )
public void dec ( <span class="methodparam">void )
public int <span class="methodname">get ( void )
public void inc ( <span class="methodparam">void )
public void lock ( <span class="methodparam">void )
public void
set ( <span
class="type">int $value )
public void unlock ( <span class="methodparam">void )
}
pht\AtomicInteger::__construct
AtomicInteger creation
说明
public <span
class="type">AtomicInteger <span
class="methodname">pht\AtomicInteger::__construct ([ <span
class="methodparam">int $value<span
class="initializer"> = 0 ] )
Handles the creation of a new atomic integer.
参数
value
The value to initialise the atomic integer to.
返回值
No return value.
范例
示例 #1 Creating a new atomic integer
<?php
use pht\AtomicInteger;
$atomicInteger = new AtomicInteger(100);
var_dump($atomicInteger->get());
以上例程会输出:
int(100)
pht\AtomicInteger::dec
Decrements the atomic integer's value by one
说明
public void pht\AtomicInteger::dec ( <span class="methodparam">void )
This method will decrement the atomic integer's value by one. Internally, the mutex lock of the atomic integer will be acquired, and so there is no need to manually acquire it (unless this operation needs to be grouped with other operations on the same atomic integer - see the example in pht\AtomicInteger::lock for a demonstration of this).
参数
此函数没有参数。
返回值
No return value.
范例
示例 #1 Decrementing an atomic integer's value
<?php
use pht\AtomicInteger;
$atomicInteger = new AtomicInteger();
$atomicInteger->dec();
$atomicInteger->dec();
var_dump($atomicInteger->get());
以上例程会输出:
int(-2)
pht\AtomicInteger::get
Gets the atomic integer's value
说明
public int <span class="methodname">pht\AtomicInteger::get ( <span class="methodparam">void )
This method will fetch the current value of the atomic integer. Internally, the mutex lock of the atomic integer will be acquired, and so there is no need to manually acquire it (unless this operation needs to be grouped with other operations on the same atomic integer - see the example in pht\AtomicInteger::lock for a demonstration of this).
参数
此函数没有参数。
返回值
The current integer value of the atomic integer.
范例
示例 #1 Getting an atomic integer's value
<?php
use pht\AtomicInteger;
$atomicInteger = new AtomicInteger(2);
var_dump($atomicInteger->get());
以上例程会输出:
int(2)
pht\AtomicInteger::inc
Increments the atomic integer's value by one
说明
public void pht\AtomicInteger::inc ( <span class="methodparam">void )
This method will increment the atomic integer's value by one. Internally, the mutex lock of the atomic integer will be acquired, and so there is no need to manually acquire it (unless this operation needs to be grouped with other operations on the same atomic integer - see the example in pht\AtomicInteger::lock for a demonstration of this).
参数
此函数没有参数。
返回值
No return value.
范例
示例 #1 Incrementing an atomic integer's value
<?php
use pht\AtomicInteger;
$atomicInteger = new AtomicInteger();
$atomicInteger->inc();
$atomicInteger->inc();
var_dump($atomicInteger->get());
以上例程会输出:
int(2)
pht\AtomicInteger::lock
Acquires the atomic integer's mutex lock
说明
public void pht\AtomicInteger::lock ( <span class="methodparam">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.
参数
此函数没有参数。
返回值
No return value.
范例
示例 #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)
pht\AtomicInteger::set
Sets the atomic integer's value
说明
public void
pht\AtomicInteger::set ( <span
class="methodparam">int $value )
This method will set the value of the atomic integer. Internally, the mutex lock of the atomic integer will be acquired, and so there is no need to manually acquire it (unless this operation needs to be grouped with other operations on the same atomic integer - see the example in pht\AtomicInteger::lock for a demonstration of this).
参数
value
The value to set the atomic integer to.
返回值
No return value.
范例
示例 #1 Setting an atomic integer's value
<?php
use pht\AtomicInteger;
$atomicInteger = new AtomicInteger();
$atomicInteger->set(20);
var_dump($atomicInteger->get());
以上例程会输出:
int(20)
pht\AtomicInteger::unlock
Releases the atomic integer's mutex lock
说明
public void pht\AtomicInteger::unlock ( <span class="methodparam">void )
This method will release the mutex lock associated with the atomic integer.
参数
此函数没有参数。
返回值
No return value.
范例
示例 #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)
简介
The pht\Threaded interface is an internal interface used by the Inter-Thread Communication (ITC) data structures (pht\HashTable, <span class="classname">pht\Queue, and <span class="classname">pht\Vector). It allows those data structures to be threaded and ensures that the mutex locking API ( <span class="methodname">pht\Threaded::lock and <span class="methodname">pht\Threaded::unlock) is implemented by each of the ITC data structures. It is not implementable by userland classes (since standalone mutex locks are not exposed).
接口摘要
pht\Threaded
class pht\Threaded {
/* 方法 */
public void lock ( <span class="methodparam">void )
public void unlock ( <span class="methodparam">void )
}
pht\Threaded::lock
Acquires the mutex lock
说明
public void pht\Threaded::lock ( <span class="methodparam">void )
This method will acquire the mutex lock associated with the given class (either a pht\HashTable, <span class="classname">pht\Queue, <span class="classname">pht\Vector, or <span class="classname">pht\AtomicInteger).
参数
此函数没有参数。
返回值
No return value.
pht\Threaded::unlock
Releases the mutex lock
说明
public void pht\Threaded::unlock ( <span class="methodparam">void )
This method will unlock the mutex lock associated with the given class (either a pht\HashTable, <span class="classname">pht\Queue, <span class="classname">pht\Vector, or <span class="classname">pht\AtomicInteger).
参数
此函数没有参数。
返回值
No return value.