-
Notifications
You must be signed in to change notification settings - Fork 502
Feasibility of thread-local non-static properties on Threaded objects #873
Comments
I think this is an interesting idea, and that TLS should be explored more. With pht, I've made everything TLS-only, but still provided the ability to communicate between threads with specific data structures. With pthreads, we could look into having a balance by giving the ability to share properties between threads (and therefore incurring serialisation overhead/limitations), as well as having properties that are TLS only (by doing something similar to what you suggested). Or perhaps each Regarding your idea, I'm unsure about the implementation (including the performance considerations of it). I don't really have time to look into this at the moment, either. It would be nice to hear some further ideas around this area, though, as well as perhaps seeing a few proof-of-concept patches to play around with. |
I haven't thought too much about how this would actually be implemented just yet. Language level support would be a godsend, but in its absence I can only come up with strange hacks and workarounds. I'll have to think about this more. I also considered magic properties, but those could result in astonishing behaviour for other developers. At the moment I resorted to static properties containing SplObjectStorages indexed by Threaded, but this isn't ideal as can be imagined. |
Just pushed a first draft on a07d15a. Supported annotations for properties are
|
Holy crap that's awesome. Nice work @sirsnyder, can't wait to test this. |
@sirsnyder |
[With some fixes for compilation] It also crashes when accessing class properties which are not public because <?php
class Test extends Thread {
public function run(){
var_dump($this);
}
protected $string = "hello world";
protected $array = array(1, 2, 3);
private $pstring = "world hello";
private $parray = array(3, 2, 1);
protected static $nocopy = true;
}
$test =new Test();
$test->string = strrev($test->string);
$test->start();
$test->join();
?> This code is already invalid. I can only assume the reason this test case being broken was missed is because of a combination of behavioural changes in v3 and #903 . |
You might also consider moving these checks to |
Thanks for first feedback! Ultimately I would not like to touch |
Summary
It may often be reasonable to want to store some data on a Threaded object that does not get serialized, for later accessing only on the parent thread - for example in a worker task based architecture where the task has a generic
onCompletion()
function which may want to operate on objects on the main thread.Currently pthreads doesn't provide any reasonable way to do this (static properties are unsuitable because different tasks of the same classes may want to store different data).
I am wondering on the feasibility of implementing some kind of
@thread_local
annotation which could be applied toThreaded
member fields to make them suitable for storing objects like this, or possibly aThreadLocal
interface if this isn't possible (although I think it would be better to be able to apply this to non-objects too, for example resources).Currently it is necessary to separately store any objects that a worker task may want to operate on after completion, because they will become serialized if assigned to the task object itself.
Example Code
This is a bad example, but I think it demonstrates the point well enough to work with.
The text was updated successfully, but these errors were encountered: