pthreads
- 简介
- 安装/配置
- 预定义常量
- Threaded — Threaded 类
- Threaded::chunk — 操作
- Threaded::count — 操作
- Threaded::extend — Runtime Manipulation
- Threaded::isRunning — 状态检测
- Threaded::isTerminated — 状态检测
- Threaded::merge — 操作
- Threaded::notify — 同步控制
- Threaded::notifyOne — Synchronization
- Threaded::pop — 操作
- Threaded::run — 执行
- Threaded::shift — Manipulation
- Threaded::synchronized — 同步控制
- Threaded::wait — Synchronization
- Thread — Thread 类
- Thread::getCreatorId — 识别
- Thread::getCurrentThread — 识别
- Thread::getCurrentThreadId — 识别
- Thread::getThreadId — 识别
- Thread::isJoined — 状态监测
- Thread::isStarted — 状态检测
- Thread::join — 同步
- Thread::start — 执行
- Worker — Worker 类
- Worker::collect — Collect references to completed tasks
- Worker::getStacked — 获取剩余的栈大小
- Worker::isShutdown — 状态检测
- Worker::shutdown — 关闭 Worker
- Worker::stack — 将要执行的任务入栈
- Worker::unstack — 将要执行的任务出栈
- Collectable — The Collectable interface
- Collectable::isGarbage — Determine whether an object has been marked as garbage
- Pool — Pool 类
- Pool::collect — 回收已完成任务的引用
- Pool::__construct — 创建新的 Worker 对象池
- Pool::resize — 改变 Pool 对象的可容纳 Worker 对象的数量
- Pool::shutdown — 停止所有的 Worker 对象
- Pool::submit — 提交对象以执行
- Pool::submitTo — 提交一个任务到特定的 Worker 以执行
- Volatile — The Volatile class

User Contributed Notes 6 notes
anonymous at example dot com ¶
5 years ago
Here are some notes regarding PHP pThreads v3 that I have gathered:
-namespace: It does not understand namespaces.
-globals: It won't serialize GLOBALS at all! And will not register new ones.
-classes: It registers new classes okay.
-functions: Will not register ANY functions - they must all be in static classes. It does understand PHP native functions.
-consts: previous constants will transfer over. Don't make any new ones thou!
-pThreads only work in CLI - of course!
-If a thread crashes it automatically gets recreated.
-In order to 'force kill' any thread the parent must be killed. Or wait until all other tasks queued are complete and then terminate.
-Anything registered in a pThread does not seem to join the main thread ... which is good!
-pThreads seem to be very powerful on multi-core environments, just need to be careful on system resources... it can and will lock up a system if mis-configured.
-Finally, finding help for PHP pThreads is slim to none... especially v3!
Good luck!
meadowsjared at gmail dot com ¶
1 year ago
In this example, it shows how to use a threaded with a pool to get an array of results, using pThreads v3.2.1 and php 7.3.23
<?php
class TestWork extends Threaded {
//updated version that works with pThreads v3.2.1 and php 7.3.23
protected $complete;
//$pData is the data sent to your worker thread to do it's job.
public function __construct($pData) {
//transfer all the variables to local variables
$this->complete = false;
$this->testData = $pData;
}
//This is where all of your work will be done.
public function run() {
usleep(2000000); //sleep 2 seconds to simulate a large job
$this->complete = true;
}
public function isDone() {
return $this->complete;
}
}
class ExamplePool extends Pool {
public $data = array(); // used to return data after we're done
private $numTasks = 0; // counter used to know when we're done
/**
* override the submit function from the parent
* to keep track of our jobs
*/
public function submit(Threaded $task) {
$this->numTasks++;
parent::submit($task);
}
/**
* used to wait until all workers are done
*/
public function process() {
// Run this loop as long as we have
// jobs in the pool
while (count($this->data) < $this->numTasks) {
$this->collect(function (TestWork $task) {
// If a task was marked as done
// collect its results
if ($task->isDone()) {
$tmpObj = new stdclass();
$tmpObj->complete = $task->complete;
//this is how you get your completed data back out [accessed by $pool->process()]
$this->data[] = $tmpObj;
}
return $task->isDone();
});
}
// All jobs are done
// we can shutdown the pool
$this->shutdown();
return $this->data;
}
}
$pool = new ExamplePool(3);
$testData = 'asdf';
for($i=0;$i<5;$i++) {
$pool->submit(new TestWork($testData));
}
$retArr = $pool->process(); //get all of the results
echo '<pre>';
print_r($retArr); //return the array of results (and maybe errors)
echo '</pre>';
?>
admin at deosnet dot com ¶
7 years ago
Hello,
WARNING : When using Stackable objects in callable functions by your Threads, you must be very careful if you use it as an array. Indeed, if you do not copy your Stackable "array" in a local variable, the execution time can drop drastically !
Also, if you want to modify an array() in a function, you will also store in a local variable in order that your array is in a thread-safe context.
jasonrlester at yahoo dot com ¶
8 years ago
Note that this extension *is* a high level implementation of POSIX threads, including on Windows (which is why pthreadsV*.dll is required)
r3x at engelhardt-stefan dot de ¶
6 years ago
If you try to test threading, remember to let php think slow:
Skript: -- C:\Webserver\htdocs>php mttest.php
<?php
class My extends Thread{
function run(){
for($i=1;$i<10;$i++){
echo Thread::getCurrentThreadId() . "\n";
sleep(2); // <------
}
}
}
for($i=0;$i<2;$i++){
$pool[] = new My();
}
foreach($pool as $worker){
$worker->start();
}
foreach($pool as $worker){
$worker->join();
}
?>
Output: -- C:\Webserver\htdocs>php mttest.php
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
If you leave sleep() out, the cpu-time for the threads is long enough to complete the script at once.
mckaygerhard at gmail dot com ¶
3 years ago
mayority of that problems only happned for guindows, .. porr os