pmmp / ext-pmmpthread

Fork of https://github.com/krakjoe/pthreads with a revamped API and PHP 8.1+ support
Other
81 stars 17 forks source link

Make a concrete Thread type that accepts Closure, rather than being extended #78

Open dktapps opened 2 years ago

dktapps commented 2 years ago

One of the biggest obstacles to dropping class copying is that we wouldn't be conveniently able to use threads in single-file scripts anymore. In addition, having to declare a whole class just to run a couple of lines of code is quite annoying and cumbersome.

This would be eased if we could run threads using just closures, similar to ext-parallel. A compatibility layer could then use closures in this manner to preserve backwards compatibility:

$task = new SomeTask();
$worker->stack(fn() => $task->run());

Similar limitations would have to be imposed on the usable closures, like in parallel, such as disallowing declaration of classes.

dktapps commented 2 years ago

It would probably be best to initially introduce this concept with ClosureThread and ClosureThreadedRunnable, to allow gradual migration.