php does not support multi-threaded how do

 

PHP by default does not support multi-threaded, multi-threaded to use pthread extensions need to be installed, but to install pthread extensions, you must use the --enable-maintainer-zts parameters recompile PHP, this parameter is specified using a thread-safe manner when compiling PHP.

Thread Safety

Multithreading is a factor in the program to become restless, prior to use multiple threads, thread-safety issues to consider first:

Security Thread: Thread safety is a term programming refers to a function, when the library is called in a multithreaded environment, able to correctly handle variable shared between multiple threads, make the program function properly completed.

In a conventional multiple threads, multiple threads shared variables, so may result in the following problems:

  1. There is a global array $ arr = array ( 'a') ;;
  2. A thread obtains an array of length 1;
  3. Obtaining a length of an array of thread B 1;
  4. A thread pop out array element $ a = array_pop ($ arr); $ a = 'a' ;;
  5. Thread B also pop array element $ b = array_pop ($ arr); $ a = null ;;
  6. At this time, the thread B appeared Supernatural, obviously array length is greater than 0, or not pop out of things;

PHP implementation

Thread-safe PHP implemented mainly using TSRM mechanism for global variables and static variables are isolated, the global variables and static variables for each thread a copy of each thread used is a backup of the main thread, thereby avoiding a variable conflict, there is a thread safety problem does not arise.

PHP multithreading package to ensure the security thread, the programmer need not consider global variables plus a variety of read-write locks to avoid conflict, but also reduces the chance of error, write more secure code.

But the resulting is that once you start running the child thread, the main thread can no longer run sub-thread details adjusted, it lost the ability for messaging between threads thread through the global variable to some extent.

At the same time PHP open thread-safe option, there will be additional losses when using TSRM mechanism for allocating and using variables, so do not need a multi-threaded environment in PHP, using PHP's ZTS (non-thread-safe) version just fine.

Classes and methods

PHP thread packaged in a Thread class, create a thread to achieve by instantiating a thread object, the use of encapsulation classes, variables can only be passed by the constructor, and the thread computation results need to pass class variable spread.

Here are several commonly used method of the Thread class:

  • run (): This method is an abstract method, each thread must implement this method, after the thread starts running this code in the method is performed automatically;
  • start (): This method is called in the main thread to start running a thread;
  • join (): various threads to the main thread are performed asynchronously, calling this method will wait for the end of the thread execution;
  • kill (): Forced end of the thread;
  • Will return true return threads running state, the thread is executing code run () method;: isRunning ()

Because thread-safe implementation, after the multi-threaded PHP running, it can no longer communicate via shared memory space, thread through the inter-thread communication can not be reused, so I believe that PHP's "thread pool" does not make sense. Pool comes with the extended class is a multi-threaded distribution management classes, there is no more introduced.

Guess you like

Origin www.cnblogs.com/IT-SUJIU/p/11765477.html