
- Lua Tutorial
- Lua - Home
- Lua Basics
- Lua - Overview
- Lua - Environment
- Lua - Basic Syntax
- Lua - Comments
- Lua - Print Hello World
- Lua - Variables
- Lua - Data Types
- Lua - Operators
- Lua - Loops
- Lua - Generic For
- Lua - Decision Making
- Lua - Date and Time
- Lua Functions
- Lua - Functions
- Lua - Multiple Results
- Lua - Named Arguments
- Lua - Default/Optional Arguments
- Lua - Closures
- Lua - Uses of Closures
- Lua - Local Functions
- Lua - Anonymous Functions
- Lua - Functions in Table
- Lua - Proper Tail Calls
- Lua Strings
- Lua - Strings
- Lua - String Concatenation
- Lua - Loop Through String
- Lua - String to Int
- Lua - Split String
- Lua - Check String is NULL
- Lua Arrays
- Lua - Arrays
- Lua - Multi-dimensional Arrays
- Lua - Array Length
- Lua - Iterating Over Arrays
- Lua - Slicing Arrays
- Lua - Sorting Arrays
- Lua - Merging Arrays
- Lua - Sparse Arrays
- Lua - Searching Arrays
- Lua - Resizing Arrays
- Lua - Array to String Conversion
- Lua - Array as Stack
- Lua - Array as Queue
- Lua - Array with Metatables
- Lua - Immutable Arrays
- Lua - Shuffling Arrays
- Lua Iterators
- Lua - Iterators
- Lua - Stateless Iterators
- Lua - Stateful Iterators
- Lua - Built-in Iterators
- Lua - Custom Iterators
- Lua - Iterator Closures
- Lua - Infinite Iterators
- Lua - File Iterators
- Lua - Table Iterators
- Lua - Numeric Iterators
- Lua - Reverse Iterators
- Lua - Filter Iterators
- Lua - Range Iterators
- Lua - Chaining Iterators
- Lua Tables
- Lua - Tables
- Lua - Tables as Arrays
- Lua - Tables as Dictionaries
- Lua - Tables as Sets
- Lua - Table Length
- Lua - Table Iteration
- Lua - Table Constructors
- Lua - Loop through Table
- Lua - Merge Tables
- Lua - Nested Tables
- Lua - Accessing Table Fields
- Lua - Copy Table by Value
- Lua - Get Entries from Table
- Lua - Table Metatables
- Lua - Tables as Objects
- Lua - Table Inheritance
- Lua - Table Cloning
- Lua - Table Sorting
- Lua - Table Searching
- Lua - Table Serialization
- Lua - Weak Tables
- Lua - Table Memory Management
- Lua - Tables as Stacks
- Lua - Tables as Queues
- Lua - Sparse Tables
- Lua Lists
- Lua - Lists
- Lua - Inserting Elements into Lists
- Lua - Removing Elements from Lists
- Lua - Iterating Over Lists
- Lua - Reverse Iterating Over Lists
- Lua - Accessing List Elements
- Lua - Modifying List Elements
- Lua - List Length
- Lua - Concatenate Lists
- Lua - Slicing Lists
- Lua - Sorting Lists
- Lua - Reversing Lists
- Lua - Searching in Lists
- Lua - Shuffling List
- Lua - Multi-dimensional Lists
- Lua - Sparse Lists
- Lua - Lists as Stacks
- Lua - Lists as Queues
- Lua - Functional Operations on Lists
- Lua - Immutable Lists
- Lua - List Serialization
- Lua - Metatables with Lists
- Lua Modules
- Lua - Modules
- Lua - Returning Functions from Modules
- Lua - Returning Functions Table from Modules
- Lua - Module Scope
- Lua - SubModule
- Lua - Module Caching
- Lua - Custom Module Loaders
- Lua - Namespaces
- Lua - Singleton Modules
- Lua - Sharing State Between Modules
- Lua - Module Versioning
- Lua Metatables
- Lua - Metatables
- Lua - Chaining Metatables
- Lua - Proxy Tables with Metatables
- Lua - Use Cases for Proxy Table
- Lua - Delegation and Tracing via Proxy Tables
- Lua - Metatables vs Metamethods
- Lua - Fallback Mechanisms in Metatables
- Lua - Fallback Cases for Indexing Metamethods
- Lua - Fallback Cases for Arithmetic and Comparison Metamethods
- Lua - Fallback Cases for Other Metamethods
- Lua - Customizing Behavior with Metatables
- Lua - Controlling Table Access
- Lua - Overloading Operators
- Lua - Customizing Comparisons
- Lua - Making a Table Callable
- Lua - Customizing String Representation
- Lua - Controlling Metatable Access
- Lua Coroutines
- Lua - Coroutines
- Lua - Coroutine Lifecycle
- Lua - Communication Between Coroutines
- Lua - Coroutines vs Threads
- Lua - Chaining Coroutines
- Lua - Chaining Coroutines With Scheduler
- Lua - Chaining Coroutines Using Queues
- Lua - Coroutine Control Flow
- Lua - Nested Coroutines
- Lua File Handling
- Lua - File I/O
- Lua - Opening Files
- Lua - Modes for File Access
- Lua - Reading Files
- Lua - Writing Files
- Lua - Closing Files
- Lua - Renaming Files
- Lua - Deleting Files
- Lua - File Buffers and Flushing
- Lua - Reading Files Line by Line
- Lua - Binary File Handling
- Lua - File Positioning
- Lua - Appending to Files
- Lua - Error Handling in File Operations
- Lua - Checking if File exists
- Lua - Checking if File is Readable
- Lua - Checking if File is Writable
- Lua - Checking if File is ReadOnly
- Lua - File Descriptors
- Lua - Creating Temporary Files
- Lua - File Iterators
- Lua - Working with Large Files
- Lua Advanced
- Lua - Error Handling
- Lua - Debugging
- Lua - Garbage Collection
- Lua - Object Oriented
- Lua - Web Programming
- Lua - Database Access
- Lua - Game Programing
- Sorting Algorithms
- Lua - Bubble Sort
- Lua - Insertion Sort
- Lua - Selection Sort
- Lua - Merge Sort
- Lua - Quick Sort
- Searching Algorithms
- Lua - Linear Search
- Lua - Binary Search
- Lua - Jump Search
- Lua - Interpolation Search
- Regular Expression
- Lua - Pattern Matching
- Lua - string.find() method
- Lua - string.gmatch() method
- Lua - string.gsub() method
- Lua Useful Resources
- Lua - Quick Guide
- Lua - Useful Resources
- Lua - Discussion
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. |