pthreads
- Введение
- Установка и настройка
- Предопределенные константы
- Threaded — The Threaded class
- Threaded::chunk — Manipulation
- Threaded::count — Manipulation
- Threaded::getTerminationInfo — Error Detection
- Threaded::isRunning — State Detection
- Threaded::isTerminated — State Detection
- Threaded::isWaiting — State Detection
- Threaded::lock — Synchronization
- Threaded::merge — Manipulation
- Threaded::notify — Synchronization
- Threaded::pop — Manipulation
- Threaded::run — Execution
- Threaded::shift — Manipulation
- Threaded::synchronized — Synchronization
- Threaded::unlock — Synchronization
- Threaded::wait — Synchronization
- Thread — The Thread class
- Thread::detach — Execution
- Thread::getCreatorId — Identification
- Thread::getCurrentThread — Identification
- Thread::getCurrentThreadId — Identification
- Thread::getThreadId — Identification
- Thread::globally — Execution
- Thread::isJoined — State Detection
- Thread::isStarted — State Detection
- Thread::join — Synchronization
- Thread::kill — Execution
- Thread::start — Execution
- Worker — The Worker class
- Worker::getStacked — Stack Analysis
- Worker::isShutdown — State Detection
- Worker::isWorking — State Detection
- Worker::shutdown — Synchronization
- Worker::stack — Stacking
- Worker::unstack — Stacking
- Modifiers — Method Modifiers
- Pool — The Pool class
- Pool::collect — Collect references to completed tasks
- Pool::__construct — Creates a new Pool of Workers
- Pool::resize — Resize the Pool
- Pool::shutdown — Shutdown all Workers
- Pool::submit — Submits an object for execution
- Pool::submitTo — Submits an object for execution
- Mutex — The Mutex class
- Mutex::create — Create a Mutex
- Mutex::destroy — Destroy Mutex
- Mutex::lock — Acquire Mutex
- Mutex::trylock — Attempt to Acquire Mutex
- Mutex::unlock — Release Mutex
- Cond — The Cond class
- Cond::broadcast — Broadcast a Condition
- Cond::create — Create a Condition
- Cond::destroy — Destroy a Condition
- Cond::signal — Signal a Condition
- Cond::wait — Wait for Condition
Коментарии
Note that this extension *is* a high level implementation of POSIX threads, including on Windows (which is why pthreadsV*.dll is required)
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.
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!
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>';
?>