# Persist context across threads?

- Thread: https://externals.io/message/60586
- Original archive: https://news-web.php.net/php.internals/60586
- Started by David Rueter on 2012-05-14T18:47:28+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #60586 — David Rueter — 2012-05-14T18:47:28+00:00

URL: https://externals.io/message/60586#60586

I am interested in preserving the complete PHP context for a thread (globals, 
variables, interpreter, etc.--everything) for later access from a different 
thread.

What would be involved in this?  It seems like:

1) Avoid calling ts_free_thread
2) Call tsrm_set_interpreter_context from new thread

Is it safe to assume that the context of a terminated thread will not be 
destroyed or overwritten as long as ts_free_thread is not called?

Thanks in advance,

David R.

---

## #60632 — Richard Lynch — 2012-05-21T19:15:55+00:00

URL: https://externals.io/message/60586#60632 · In reply to: #60586

> I am interested in preserving the complete PHP context for a thread
> (globals,
> variables, interpreter, etc.--everything) for later access from a
> different
> thread.
>
> What would be involved in this?  It seems like:
>
> 1) Avoid calling ts_free_thread
> 2) Call tsrm_set_interpreter_context from new thread
>
> Is it safe to assume that the context of a terminated thread will not
> be
> destroyed or overwritten as long as ts_free_thread is not called?

I don't really know what I'm talking about, but nobody has answered
yet...

For starters, not all extensions are thread-safe. I would expect that
to mean that anything actively using those extensions saved state in
another thread is a segfault waiting to happen.

For security reasons and sheer plain cussedness, I don't think you're
going want and/or get any resource (mysql connection/result/etc, gd
images, file handles, etc) survive to the next thread.  A mysql
connection, for example, is opened with a specific user/pass. Another
thread picking this up could probably abuse that, as I understand it.

Finally, your question begs another question:

Why do you want to do this?

-- 
brain cancer update:
http://richardlynch.blogspot.com/search/label/brain%20tumor
Donate:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=FS9NLTNEEKWBE
