3 ms·
Dumb question, but is this something that a dev would expect the compiler to do for us automatically depending on what is deemed most efficient or is this somet
by KeepFlying 9y ago
Dumb question, but is this something that a dev would expect the compiler to do for us automatically depending on what is deemed most efficient or is this something that a developer can write directly into the code to make it work this way?
I want to be sure I understand what is going on here to be sure.
Can someone offer an example of where this ability would be particularly useful?
- gumby 9y agoRead the wikipedia page on coroutines first. It's a fairly basic concept in computer science. It's a different model of control structure, primarily when you have a thing that produces data and a thing that consumes it. It could be in multiple threads, multiple machines, or just a single thread, so is often included in multiprocessing portions of designs, standards and instruction.
- ioquatix 9y agoActually, a coroutine is just a more generalised function - it's a function which retains it's state over multiple calls. I recommend watching this video, which has a great overview. https://www.youtube.com/watch?v=_fu0gx-xseY https://www.youtube.com/watch?v=_fu0gx-xseY
- Twisol 9y agoCoroutines are quite nice for managing sessions with asynchronous events, like stateful GUIs or client-server interactions. If you're familiar with the tendency of callbacks in e.g. JavaScript to nest deeply, you can think of coroutines as a way to recover a flat, procedural style. I'm a particular fan of how coroutines work in Lua. Here's an article that helps explain them a bit in that context: http://leafo.net/posts/itchio-and-coroutines.html http://leafo.net/posts/itchio-and-coroutines.html
- vvanders 9y agoYeah, they're awesome for doing sequences of events that can have temporal gaps in the middle. We used to use them(Lua) in games to do scripted sequences and AI. Was simple enough that even our designers could edit/extend them.
- Splines 9y agoI've seen useful usage of coroutines where you have a task you want to run synchronously on the main thread, but you want it to yield time without unwinding the whole stack. So you create another thread, have the main thread yield time to it, and at fixed points in this other thread yield back to the main thread. When the main thread yields to this other thread, all it's context is maintained and you don't need to "walk up" the stack to restore the work this thread was doing. At a high level, this solves the class of problems where you need to do background tasks in applications that aren't thread safe. There are probably other places where these work well, but this is just my experience.