PHP uses the multi-thread operation instance implemented by Pthread, pthread instance _ PHP Tutorial

Source: Internet
Author: User
PHP uses the Pthread-implemented multi-thread operation instance, pthread instance. The example of multi-thread operations implemented by PHP using Pthread. the example in this article describes the multi-thread operations implemented by PHP using pthread. For your reference, I would like to share with you the following details: multi-thread operation instances implemented by Pthread in php PHP and pthread instances.

This example describes the multi-threaded operations implemented by PHP using Pthread. We will share this with you for your reference. The details are as follows:

<? Php class vote extends Thread {public $ res = ''; public $ url = array (); public $ name =''; public $ runing = false; public $ lc = false; public function _ construct ($ name) {$ this-> res = 'None, run for the first time. '; $ this-> param = 0; $ this-> lurl = 0; $ this-> name = $ name; $ this-> runing = true; $ this-> lc = false;} public function run () {while ($ this-> runing) {if ($ this-> param! = 0) {$ nt = rand (1, 10); echo "thread [{$ this-> name}] receives the task parameter: {$ this-> param }, it takes {$ nt} seconds to process the data. \ n "; $ this-> res = rand (100,999); sleep ($ nt); $ this-> lurl = $ this-> param; $ this-> param = '';} else {echo" thread [{$ this-> name}] waiting for the task .. \ n ";}sleep (1) ;}}// create a thread pool here. $ pool [] = new vote ('A'); $ pool [] = new vote ('B'); $ pool [] = new vote ('C '); // start all threads to make them in the working state foreach ($ pool as $ w) {$ w-> start ();} // dispatch a task to the thread for ($ I = 1; $ I <10; $ I ++) {$ worker_content = rand (10, 99); while (true) {foreach ($ pool as $ worker) {// if the parameter is null, the thread is idle if ($ worker-> param = '') {$ worker-> param = $ worker_content; echo "[{$ worker-> name}] thread idle, put the parameter {$ worker_content }, result of last parameter [{$ worker-> lurl}] [{$ worker-> res}]. \ n "; break 2 ;}} sleep (1) ;}} echo" all threads are distributed, waiting for execution to complete. \ n "; // wait for all threads to finish running while (count ($ pool) {// traverse and check the Thread Group running to end foreach ($ pool as $ key => $ threads) {if ($ worker-> param = '') {echo" [{$ threads-> name}] the thread is idle, result of last parameter [{$ threads-> lurl}] [{$ threads-> res}]. \ n "; echo" [{$ threads-> name}] the thread is running completely and exits. \ n "; // set the end Flag $ threads-> runing = false; unset ($ pool [$ key]) ;}} echo" waiting... \ n "; sleep (1) ;}echo" all threads have been executed. \ n ";

I hope this article will help you with php programming.

Examples in this article describes the multi-threaded operations implemented by PHP using Pthread. For your reference, the details are as follows: php...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.