Why PHP developers can't provide setlocale function as per thread scope

回眸只為那壹抹淺笑 提交于 2019-12-07 20:14:54

问题


We have setlocale function in PHP. This function has warning message

The locale information is maintained per process, not per thread. If you are running PHP on a multithreaded server API like IIS or Apache on Windows, you may experience sudden changes in locale settings while a script is running, though the script itself never called setlocale(). This happens due to other scripts running in different threads of the same process at the same time, changing the process-wide locale using setlocale().

For example, apache for windows with miltithread settings. I found these lines of code in PHP project:

# ifdef PHP_WIN32
_configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
# endif

If this code is works, why we must be care about warning of setlocale function?


回答1:


You already provided the answer:

The locale information is maintained per process, not per thread. [...]

It's not a PHP problem, that's just how locale settings work. Changing it applies to the whole running process. It's a system setting that doesn't know about your application-internal threading.

Now it appears since Windows handles both the locale and the threading via system calls, it can accomplish the unthinkable task: apply the locale settings per thread.
Here's a reference: http://msdn.microsoft.com/en-us/library/ms235302(v=vs.80).aspx

So if you run on a system which is specifically prepared for that, you don't need to care. For portability of your applications you still should keep it in mind. (Albeit the Apache threading MPM isn't used that widely I believe.)



来源:https://stackoverflow.com/questions/5674519/why-php-developers-cant-provide-setlocale-function-as-per-thread-scope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!