How to use pthreads in php
Editor to share with you how to use pthreads in php, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
How to use php pthreads: 1. Install pthreads;2 through "pecl install pthreads" and use mutex locks when you need to control that multiple threads can only work with one thread at a time.
This article operating environment: windows7 system, php7.0.2 version, DELL G3 computer
Installation and use of php multithreaded pthreads
Installing Pthreads basically requires recompiling PHP, plus the-- enable-maintainer-zts parameter, but using this document is rare; bug will have a lot of unexpected problems, so the generation environment can only be hehe, so just play with this thing, the real multithreading is still using Python, C, and so on.
Most of the following code comes from the network
I. installation
Php-7.0.2 is used here
. / configure\-- prefix=/usr/local/php7\-- with-config-file-path=/etc\-- with-config-file-scan-dir=/etc/php.d\-- enable-debug\-- enable-maintainer-zts\-- enable-pcntl\-- enable-fpm\-- enable-opcache\-- enable-embed=shared\-- enable-json=shared\-- enable-phpdbg\-- with-curl=shared\-- with-mysql=/usr/local/mysql\-- with-mysqli=/usr/ Local/mysql/bin/mysql_config\-with-pdo-mysqlmake & & make install
Install pthreads
Pecl install pthreads II, Thread III, Worker and StackableStackables are tasks that are executed by Worker threads. You can synchronize with, read, and write Stackable objects before, after and during their execution.
4. Mutex
Under what circumstances will mutexes be used? It can be used when you need to control multiple threads and only one thread can work at a time. A simple counter program that explains the difference with or without mutexes
Multithreading and shared memory
In the case of shared memory, no locks are used and may still work properly, and the working memory operation itself may have the function of locking.
Fifth, thread synchronization
There are scenarios where we don't want thread- > start () to start running the program, but rather want the thread to wait for our command. $thread- > wait (); the thread does not run immediately after the test function is thread- > start (), but only after receiving the signal of $thread- > notify ();
VI. Thread pool
A Pool class
Dynamic queue thread pool
The above example is to perform start unified startup when the thread pool is full, and the following example is to create a new thread as soon as there is free in the thread pool.
Pthreads Pool class