2

I'm devoloping a Drupal module, in these module I need to execute for 5 times the same part of code, so, I think that I can do it in parallel to increase speed of execution, but I don't want to use pcntl_fork, I would like to use pthread. Now the question is: are pthread library for php the same as in c? Can I implement kernel level threads in php? Because I think that if php pthread are user level I don't have any advantage to use it in my case Thank for replies

1 Answers1

7

First, some actual facts:

The pthreads API facilitates multi-threading in user-land: It does not create green, or user threads.

Green threads are implemented as a kind of co-operative, or preemptive multitasking in languages whose virtual machines or environments are not well prepared for truly multi-threaded execution.

PHP is prepared for multi-threaded execution, it does have a proper threading model, and so green threads are not necessary.

pthreads in PHP are not green threads, they are implemented with Posix Threads, which are kernel threads

http://en.wikipedia.org/wiki/Green_threads

http://en.wikipedia.org/wiki/Kernel_thread

The question at hand

I'll assume that you intend for these threads to be executed by the frontend (that is, within the webserver at the request of a client).

I implore you not to do this; basic maths prohibit a model of execution where a request results in X threads being scheduled from scaling.

If part of your application requires what threading provides then separate out that part of the application from the frontend (web server), isolate it completely, execute it as a system service in communication with your frontend via some sane form of RPC.

Joe Watkins
  • 17,032
  • 5
  • 41
  • 62
  • Ok perfect, now I have understand what pthreads do in php thanks a lot! Now I explane in a better way what I did with pthreads, I have a page that inside there 5 selects, for each select I must take some values from Active Directroy, the results for each query are a lot, so I want to run in parallel the part for build each select.when the client request the page to web server, start the script where is the parallel part, then I must to remove it or manage it with RPC. right? thanks for your reply! – Riccardo Mariani Aug 25 '14 at 09:35
  • But I understand what you want to say, that the best practice for implements threads in this situation is to separate the threaded part from web server. thank a lot again – Riccardo Mariani Aug 25 '14 at 10:57