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 ?
Every help/advice is welcome :)
Regards.
Simon
Hi Simon,
-----Original Message-----
From: simon .barotte [mailto:simon.barotte@gmail.com]
Sent: Tuesday, December 6, 2016 10:49 AM
To: internals@lists.php.net
Subject: [PHP-DEV] Use pthread in php extensionHi 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
Hi!
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.
Besides that, many libraries (e.g. ICU4C) have either non-thread-safe
parts (e.g. functions with global state) or special actions need to be
performed to use them in thread-safe manner, which PHP will not perform
when built in non-ZTS mode. So you need to be careful with third-party
libraries too.
--
Stas Malyshev
smalyshev@gmail.com