Learning notes for passing PHP Multithreading (pthreads) parameters

Source: Internet
Author: User
Tags abstract anonymous constructor php multithreading

Test Environment

OS: win7 64
PHP: 5.4.25 ts
Pthreads: 0.1.0


1. Performance problems

2. Some types such as PDO cannot be passed to the thread because they cannot be serialize.
These two problems can be solved through other solutions. The solution is benevolent and wise.

In addition, anonymous functions can be passed to the thread, but there is a bug. Anonymous functions cannot be assigned to attributes in the thread, the passed anonymous functions can only be used in the thread's constructor (call_user_fun * series function calls ).

The Thread attribute must be initialized in the constructor; otherwise, it must be null. The code in _ construct () and run () is not in the same dimension. If the attribute is not a PHP scalar, it cannot be modified in run (), for example, initializing an object in the constructor, then, modifying object attributes in run () does not take effect.

Correct syntax:

The code is as follows: Copy code

Abstract class Task extends Thread {
Private $ finished;
Public $ terminated;
Protected $ id;
Public $ terminate;
Public function _ construct ($ id ){
$ This-> id = $ id;
$ This-> terminated = true;
$ This-> finished = false;
$ This-> terminate = false;
 }
}

Incorrect syntax:

The code is as follows: Copy code

Abstract class Task extends Thread {
Private $ finished = false;
Public $ terminated = false;
Protected $ id; // www.111cn.net
Public $ terminate = false;
Public function _ construct ($ id ){
$ This-> id = $ id;
 }
}


This is also wrong, regardless of stdClass or array

The code is as follows: Copy code
Abstract class Task extends Thread {
Private $ info;
Public function _ construct ($ task ){
$ This-> info = array ();
$ This-> info ['task'] = $ task;
$ This-> info ['finished'] = false;
$ This-> info ['terminate'] = false;
$ This-> info ['termination'] = false;
$ This-> info ['error'] = false;
$ This-> info ['info'] = array ();
 }
}


Later studies found that the overall value assignment of composite data seems to work.

If the program has a lot of callback functions used inside the thread, it will find it dead.

 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.