Parallelism mechanisms in Lua

This section covers an interesting parallelism mechanism in Lua called coroutines that will help us achieve collaborative multitasking.

Coroutines

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 ...

Get Mastering the Nmap Scripting Engine now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.