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