Lua - Coroutines vs Threads



Coroutines and Threads are concurrency mechanisms. Lua supports coroutines by default and threads support can be added with the help of third party library like luasocket or Lua's Foreign Function Interface, FFI. In this chapter, we're comparing coroutines and threads with details.

Threads

  • True Parallelism − Threads runs in parallel fashion and can run simaltaneously on multi-CPU systems. We can run different part of our application's at same time.

  • Preemptive Multitasking − Operating System's scheduler is the main player who switches between threads. A thread can be interrupted at any time even it is not asked explicitly.

  • Synchronization − Threads share a common memory space. This helps in data sharing but it also need a careful synchronization to prevent race conditions and maintain data integrity.

  • Overhead − Creating and switching threads while maintaining related context can be a expensive affair for operating system.

  • Concurrency Model − Threads are truely concurrent and on multi-cpu systems, multiple threads can work in same instant.

  • Error Handling − Threads are generally independent. If one thread fails, it may not impact the entire application process depending upon how errors are handled.

Coroutine

  • Cooperative Multitasking − Coroutines works in a single thread. Concurrency is achieved by voluntarily yielding control to each other. A coroutine runs until coroutine.yield() is called or it finishes execution.

  • No true Parallelism − Coroutines do not work in parallel. At a time, only one coroutine is active. A coroutine has to program in such a way that it appears the code is concurrent although it is interleaved.

  • Explicit Control − Lua programmer explicity decide when to pause a coroutine and run another coroutine. This provides a fine-grained control to the developers.

  • Lightweight − In terms of resources and context switching, coroutines are quite Lightweight as compared to threads.

  • Shared Memory − Coroutines works in same memory space and being in same thread, sharing data between coroutines is quite simpler and easier than in threads. No explicit locking mechanism is needed for synchronization.

  • Concurrency Model − Coroutine can be used in concurrent like form for I/O bound tasks or managing state machines where we need to pause and resume operations explicitly. This improves responses of the application.

  • Error Handling − An error in a coroutine can propagate to main thread and has to be handled properly.

Differences between Metatable and Metamethod

Following table summarises the differences between a metatable and a metamethod.

Sr.No. Feature Threads Coroutines
1 Parallelism Threads exhibits true parallelism on multi-core systems. Coroutines are interleaved in a single threaded program. Thus no true parallelism.
2 Multitasking Operating System Scheduler's decides threads execution and switching and this makes threads Multitasking as Preemptive. Coroutines are controlled by program and thus multitasking is cooperative by nature.
3 Context Switching Operating System based context switching is relatively expensive. Coroutines are very lightweight being withing Lua VM.
4 Usage of Resources Being Operating System based, thread resource usage is higher. Coroutines are Lua based objects and this resource usage is low.
5 Need of Synchronization Synchronization is required to maintain integrity of shared data. Coroutines being single threaded, synchronization is not required as such.
6 Locking Complexity Threads often needs a complex locking mechanism to manage control flow. Coroutines are simpler to manage control flow.
7 Use Cases Threads are applicable for CPU Intensive operations and where true concurrency is required to improve performance. Coroutines are useful for I/O bound tasks, state machines and cooperative multitasking.
Advertisements