What are Light Weight Threads in context of Goroutines?

Sankalp polSankalp pol
2 min read

A thread referred as the smallest unit of execution within a process, is managed by the OS Kernel. Each Thread has its own stack i.e. its own reserved memory for its state. Depending on the Operating System, the thread stack size can vary but typically falls between 1MB and 8MB.

Light Weight Threads often referred as User Level Threads were first conceptualized and developed in 1960s and 1970s. These are managed by a user-space library rather than the OS Kernel. The kernel isn't aware of the Light Weight Threads being created.

In context of Operating Systems, Light Weight Threads can have a different meaning. We are defining LWT in context of Programming Languages.

Light Weight Threads run above the OS Threads and their call stack is in most cases smaller than OS Threads.

Creating Light Weight Threads is also cheap and fast for following reasons:

  1. When OS creates threads, it also has to do some extra work like managing the state of the thread. Light Weight Threads are switched at user level so there is less operational overhead.

  2. Light Weight Threads also typically have smaller stack size.

  3. Multiple Light Weight Threads can use a single OS Level Thread. This is called as multiplexing.

  4. Light Weight Threads enable concurrent execution of multiple tasks independently but without the overhead of OS thread management.

Programming languages such as Go use light weight threads for Goroutines. Go also uses multiplexing allowing multiple Goroutines to use a single OS Thread. For e.g. in I/O-Bound operations, you can maybe run 1000+ Goroutines on a single thread while for CPU Heavy operations, the ratio of multiplexing will be lesser.

Depending on the Go Runtime version, Goroutines typically have an initial stack of 2kb which grows dynamically. Goroutines are handled & scheduled by the Go Runtime Scheduler.

Java introduced virtual threads, which were lightweight threads managed by the JVM. This was introduced through Project Loom (Java 19+ Preview / Java 21+ Stable). These as well are multiplexed by the JVM, running on a small pool of carrier OS Threads.

Other modern languages like Kotlin, Rust (async), and Erlang also use similar lightweight threading/concurrency models.

0
Subscribe to my newsletter

Read articles from Sankalp pol directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sankalp pol
Sankalp pol