What is Coroutines
Coroutine basically is general control structures where flow control is cooperatively passed between two different routines while returning. They are computer program components that generalize subroutines for non-preemptive multitasking, by allowing execution to be suspended and resumed.
Coroutines are well-suited for implementing familiar program components such as cooperative tasks, exceptions, event loops, iterators, and infinite lists. To know the in-depth definition, click here.
Why Coroutines is Required
In order to read a file and parse it while reading into some meaningful data, one can either read it step by step at every line or may also load the entire content in memory, which would not be recommended for large text cases e.g text editors like Microsoft Word. In general, to throw away the stack concept completely, it is needed. And also when we want things to do concurrently i.e. non-preemptive multitasking, we need coroutines for concurrency.
How Coroutines Works
It rescheduled at specific points in the program and do not execute concurrently, programs using coroutines can avoid locking entirely.
This is also considered as a benefit of event-driven or asynchronous programming.
It is almost similar to threads but one main difference is that threads are typically preemptively scheduled while coroutines are not and this is because threads can be rescheduled at any instant and can execute concurrently while coroutines rescheduled at specific points and not execute concurrently.
Use Cases of Coroutines
- State Machines
- Actor Model
- Communicating Sequential Processes
- Reverse Communication
State Machines: It is useful to implement state machines within a single subroutine, where the state is determined by the current entry or exit point of the procedure. This results in the more readable code as compared to the use of goto.
Actor Model: It is very useful to implement the actor model of concurrency. Each actor has its own procedures, but they give up control to the central scheduler, which executes them sequentially.
Generators: It is useful to implement generators which are useful for streams particularly input/output and for traversal of data structures.
Communicating Sequential Processes: It is useful to implement communicating sequential processes where each sub-process is a coroutine. Channel input/output and blocking operation yield coroutines and a scheduler unblock them on completion events.
Reverse Communication: They are useful to implement reverse communication which is commonly used in mathematical software, wherein a procedure needs the using process to make a computation.
Comparison with Subroutines and Threads
Subroutines are the special cases of coroutines. When subroutines are called, execution begins at the start, and once subroutines exit, it is finished. An instance of a subroutine only returns once, and it does not hold state between invocations.
This is very much similar to threads. However, coroutines are cooperatively multitasked while threads are preemptively multitasked. This means that coroutines provide concurrency, not parallelism.
Benefits of Coroutines
1. Implement in asynchronous programming.
2. Implement functional programming techniques.
3. Implement it because of poor support for true parallelism.
4. Pre-emptive scheduling can be achieved using coroutines.
5. Keep the system’s utilization high.
6. Requires less resource than threads.
7. Resource locking is less necessary.
8. Increases locality of reference.
Learn More About Coroutines
For detailed information, Get in Touch with us for specific use cases.
You may also learn more about AresDB in this insight.