Hi Simon,

> -----Original Message-----
> From: simon .barotte [mailto:simon.baro...@gmail.com]
> Sent: Tuesday, December 6, 2016 10:49 AM
> To: internals@lists.php.net
> Subject: [PHP-DEV] Use pthread in php extension
> 
> Hi all,
> 
> I developpe an extension php, and I would like use Linux pthread in my 
> extension
> (PHP 5.6.27).
> But I have a feeling that my thread don't work, I looked for the documentation
> but I find no concrete example.
> Can I use pthread without zts activated ? Sample of my code :
> 
> void *called_from_thread(void *num){
>     php_printf(" print by thread");
> }
> 
> PHP_FUNCTION(test){
>     pthread_t inc_x_thread;
>     int x = 0;
> 
>     if(pthread_create(&inc_x_thread, NULL, called_from_thread, &x)) {
>         php_printf("Error creating thread");
>         return;
>     }
> 
>     php_printf("This is main print");
> 
>     if(pthread_join(inc_x_thread, NULL)) {
>         php_printf("Error joining thread");
>         return;
>     }
> }
> 
> When I execute my extension in php, I always obtain the same display, This is
> main print print by thread For me the normal behavior of thread would be to
> have different displays sometimes... no ?
> 
Of course you can link your extension with any possible library. Particularly 
with pthreads, when PHP is compiled non thread safe, you need to be very 
careful using the PHP APIs. Many will be not thread safe in that case. Best 
strategy were probably not using PHP APIs in the thread callbacks at all.

Probably a scenario were thinkable, to process some huge amount of data 
internally with multiple threads. But you can of course prepare some data, 
start threads in some internal PHP function, but don't return from that 
function until all the jobs are done. 

Regards

Anatol


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to