> On Dec 21, 2020, at 4:33 PM, Mike Schinkel <m...@newclarity.net> wrote:
> 
>> On Dec 17, 2020, at 11:30 AM, Aaron Piotrowski <aa...@trowski.com> wrote:
>> 
>> Hello everyone!
>> 
>> I would like to introduce an RFC for adding full-stack fibers to PHP: 
>> https://wiki.php.net/rfc/fibers
>> 
>> Fibers are primarily used to implement green-threads or coroutines for 
>> asynchronous I/O. Fibers are similar to threads, except fibers exist within 
>> a single thread and require cooperative scheduling of the fibers by the 
>> process. Since fibers do not require a full CPU context switch, they are 
>> lightweight and more performant than multi-processing or threading for 
>> awaiting I/O.
>> 
>> An implementation as an extension is at https://github.com/amphp/ext-fiber
>> 
>> Fibers are a complex feature. The RFC contains many examples and links to 
>> code using fibers to help explain and demonstrate what is possible, however 
>> I’m certain many more questions and concerns will arise. Looking forward to 
>> feedback and discussion.
>> 
> 
> This is interesting, and potentially very useful.
> 
> I am curious about how you propose access to shared memory across fibers?  
> What will happen if two fibers try to update a $GLOBALS variable at the same 
> time?  Or a property of the same object?  How will developers manage that?
> 
> -Mike
> 
> P.S. Have you considered concurrency functionality like in GoLang[1] e.g. 
> channels, where the mantra is "Do not communicate by sharing memory; instead, 
> share memory by communicating?"
> 
> [1] 
> https://medium.com/@thejasbabu/concurrency-in-go-e4a61ec96491#:~:text=Do%20not%20communicate%20by%20sharing,race%20conditions%2C%20memory%20management%20etc
>  
> <https://medium.com/@thejasbabu/concurrency-in-go-e4a61ec96491#:~:text=Do%20not%20communicate%20by%20sharing,race%20conditions%2C%20memory%20management%20etc>.

Hi Mike,

Fibers do not change the single-threaded nature of PHP. Only a single fiber can 
be running at one time, so memory cannot be modified simultaneously.

There are synchronization issues when writing asynchronous code using either 
stackless or stackful coroutines, as anyone who has worked with AMPHP or 
ReactPHP can tell you. Multiple fibers (coroutines, green-threads, whatever you 
want to call them) will interleave execution. Multiple interleaved fibers can 
change object state between pausing and resuming, which I'm guessing is more to 
what you were concerned about, rather than literal simultaneous modification 
that can occur with threads. The RFC does not provide tools to synchronize 
memory access, as these can be implemented in user space. Fibers don't provide 
the entire API, just the "bare-metal" API needed to implement various styles of 
concurrency in user space code.

AMPHP provides synchronization tools such as mutexes, semaphores, parcels, and 
channels in https://github.com/amphp/sync <https://github.com/amphp/sync> and 
https://github.com/amphp/parallel <https://github.com/amphp/parallel>. Other 
libraries could provide similar tools, though perhaps with a different 
approach. We too like the Go-style of concurrency and look to emulate it in 
AMPHP v3.

Cheers,
Aaron Piotrowski

Reply via email to