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.