Does PHP have threading?
Does PHP have threading?
Yes, PHP does support multithreading and parallel execution, although it is not built-in to the core language. To achieve multithreading in PHP, you need to use the pthreads extension.
The pthreads extension provides classes and methods for creating, managing, and synchronizing threads in PHP. It allows you to create independent units of execution called threads that can run concurrently.
Here's a simple example of how to create and manage threads using pthreads:
class PThread extends Thread {
public function run() {
$sleep = mt_rand(1, 10);
printf('Thread: %s has started, sleeping for %d'. "\n", $this->id, $sleep);
sleep($sleep);
printf('Thread: %s has finished'. "\n", $this->id);
}
}
$poolArr = array();
foreach (range("0", "3") as $i) {
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.