views:

66

answers:

1

Can anybody please describe about how to implement threading in PHP programming? What I know for sure is that PHP does not support thread concept. However, please correct me if I'm wrong.

If possible, please try to provide some details with one example about PHP threading.

If possible, can anybody please also provide some details on thread-safety of PHP modules? I know this is somewhat hot topic in the market, but without any nice & understandable details for dummies like me.

Any help is greatly appreciated.

+1  A: 

Threading is on the development roadmap for PHP, but it's not possible with current versions of PHP. There's no thread spawning functions, no thread handling functions, no thread synchronization functions, etc... As well, most PHP plugins/modules/libraries would need to be rewritten to handle threading safely, as they were not built with anything but single-threaded execution in mind.

As one contrived example... you're doing a data import script in PHP and fire off multiple threads to parse chunks of data and insert them into a database. What will mysql_insert_id() return? Since nothing's thread aware in PHP, you might very well get the ID from an insert operation performed in an entirely different thread.

You can certainly use PHP to fork(), popen(), proc_open(), etc... to up fire multiple copies of a script, and run those concurrently, but that's just multi-processing. Each will have its own environment, memory, file handles, etc...

Marc B
@Marc - Thanks for such an informative answer. But can you also please highlight about the thread-safety modules in PHP, as there's some mentioning about them, although not totally clear about those for dummies. Once again, thanks.
Knowledge Craving
Mostly they're there so PHP can run under IIS, which is a multithreaded server. But a PHP script under IIS still can't start its own threads - there's simply nothing in the language that allows it.
Marc B