Multithreading in Lua
To understand multithreading in Lua, let’s begin with a fundamental question.
How does Lua support multithreading?
Lua does not support multithreading. Period.
But we cannot finish this section yet. We will explain this further with two approaches – a contemporary one and an old-school one.
The contemporary approach
Lua is a scripting language and it does not support preemptive multithreading. It simply does not provide a library function to create a new thread, so there is no way to do it.
Nowadays, CPUs and operating systems are designed around preemptive multithreading – that is, a thread of execution can be paused and resumed at any time. A thread has no control over its execution schedule.
However, Lua provides a mechanism for cooperative multithreading with coroutines. In a cooperative multithreading environment, the thread of execution is never preempted. Only when the thread willingly gives up its execution can...