This section covers an interesting parallelism mechanism in Lua called coroutines that will help us achieve collaborative multitasking.
Coroutines in Lua are a very interesting feature that allow developers to execute multiple tasks cooperatively. Each coroutine has its own execution stack, and they are used in the background by NSE to encapsulate the execution of its scripts. The main advantage of using coroutines is the ability to suspend and yield execution of tasks. It is important to understand the difference between coroutines in Lua and traditional threads in preemptive multitasking. Coroutines share context data and, therefore, must be used to reduce overhead when working with tasks that share a lot of information. However, keep in mind that only one task is executed at any given time. Tasks must pass control among themselves to achieve collaborative multithreading.
A coroutine has three possible states:
Running
Suspended
Dead
Basically, the execution...