6

PHP apparently supports now a Thread class. There is also a Mutex and Cond class. One need nevertheless to compile it with the good options and add a PECL extension to have it.

Is there a good tutorial where to start ? Something that shows clearly the fair use of each Class / Method.

I just don't know how to deal with these classes. I would need some concrete examples of doing very simple things that exhibits as many methods as possible.

Thank you.

dader
  • 1,304
  • 1
  • 12
  • 31

2 Answers2

14

It has been released as is available on pecl, windows downloads on github pages ... git (source) if you can, more recent code ...

http://pthreads.org contains some more information, and checkout the examples folder too ...

If you're more specific in what you need to do I can provide a precise answer, feel free to contact me directly ...

Sorry about the lack of documentation, I am aggressively developing pthreads in what little time I have around work - it was written while I was on holiday from work and I am now back at work - the documentation will be updated as much as possible as soon as I find the time.

Threading is usually a complicated thing, but it needn't be ... ever seen a thread pool that is less than 100 lines ?? https://github.com/krakjoe/pthreads/blob/master/examples/Pooling.php ... there's one !!

You needn't know about conditions or mutex to operate threads in PHP, pthreads includes easy peasy everything ...

Jump on board, without question, I will perfect it ...

Joe Watkins
  • 17,032
  • 5
  • 41
  • 62
  • That was totally what I looked for, thank you. I won't hesitate to contact you as soon as I need to. – dader Oct 11 '12 at 15:14
  • 1
    Does pthread works within apache ? if so, should i take the mpm-prefork, or mpm-worker ? – dader Nov 29 '12 at 13:00
  • Thanks :) I'm not so sure that will happen, or would be a good thing, see http://stackoverflow.com/questions/209774/does-php-have-threading/14548828#14548828 for a full answer with relevant background info – Joe Watkins Jan 28 '13 at 09:14
  • Broken links .. Please fix – Baba May 07 '13 at 15:37
  • Done, moved pthreads.org to github, so using example link on gh - no time to maintain/develop pthreads.org site at the moment ... – Joe Watkins May 07 '13 at 20:11
1

Threading is not easy, in any language. It takes a different train of thought (non-linear). Since what you mentioned is not available in any released PHP version, I would recommend playing with the pctnl and shmop functionality. Specifically pcntl_fork. Both have been around a while and are supported. It is multi-processing and not threading, but may accomplish what you are looking for. But threading is more efficient.

Typically you would use forking to have a parent process check for work, delegate it to a forked process, and then check for more work while the child process does the work. The parent process can check on the status of the child processes. For example, you could make sure you only fork a certain number of child processes and wait for one to complete before forking another.

I've forked up to 500 processes at which point the CPU load got to high. The parent process would check system load and not fork any more if the load was too high.

If you really need to, you could use shared memory (shmop functions) with forking to get much of the functionality you would get with threading.

Brent Baisley
  • 12,641
  • 2
  • 26
  • 39
  • 1
    thanks for your answer. I've already played with pcntl_fork, and it does some of the work, but it seems to me that threading would save me the overhead of inter process communication ( assuming that threading provides great facilities concerning variables sharing among the different threads, am I wrong ? ). Especially, I think , when using immutable objects ( again, am I wrong ? ). – dader Oct 10 '12 at 15:33
  • You are correct, threads do save you overhead. You can do "variable sharing" with the shared memory functions without threading. Honestly, if you are looking to do threading and worried about the overhead of processes, you probably should be looking at a language besides PHP. – Brent Baisley Oct 11 '12 at 01:51
  • I totally agree with you, PHP is far from being a choice for this task. – dader Oct 11 '12 at 15:11