PHP: Sharing a static variable between threads

岁酱吖の 提交于 2019-12-05 16:46:06

Static variables are not shared among contexts, the reason is that static variables have a class entry scope, and handlers are for managing the object scope.

When a new thread is started, statics are copied (removing complex variables, like objects and resources).

The static scope can be thought of as a kind of thread local storage.

In addition, where members are not static ... all members of a class derived from a pthreads definition are considered public.

I encourage you to read the examples distributed with pthreads, they are available on github too.

How are you accomplishing multi-threading?

PHP doesn't have the same threading support as languages like Java, where you have a JVM which is constantly running at application level.

With PHP each page request is creating a new instance of PHP to handle that request, and the scope of the static variables are only for each running instance.

To share data between threads, you'll need to store value in the DB, session, or a simple file depending on your requirements.

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