PHP multithreading (pthreads) parameter transfer learning notes-PHP source code

Source: Internet
Author: User
In PHP multi-Thread programming, variables can be transmitted to the Thread through the Thread and Worker constructor, or through the public attribute or public method of the Thread. After research, we found that all variables are passed through serialize () and unserialize () Implementation transfer, this will cause several problems: in PHP multi-threaded programming, variables can be passed to the Thread through the Thread, Worker constructor, it can also be implemented through the public attribute of the thread or the public method. After research, it is found that all are passed through serialize () and unserialize (), which will cause several problems:

Script ec (2); script

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:

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:

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:
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.

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.