Skip to main content
Definition Website Computer and Technology

Green Thread

Subject: Computer and Technology noun /ˈɡriːn ˌθrɛd/ 1 definition

  1. Definition (noun)

    Entry #351 · Updated

    A Green Thread is a lightweight, user-space thread of execution scheduled and managed by a programming language runtime, virtual machine, or user-level library rather than by the operating system kernel. Instead of mapping every logical task directly to a kernel thread with a large fixed stack and kernel-mediated context switches, the runtime maintains small or dynamically resizable stacks in user memory and multiplexes many green threads onto one or more OS carrier threads. Whenever a green thread yields, blocks on a runtime synchronization primitive, or performs non-blocking I/O wired to kernel event notification (such as epoll, kqueue, or io_uring), the user-space scheduler saves only essential CPU registers and stack pointers and dispatches another runnable task without a kernel trap. Its core purpose is enabling massive concurrency—scaling to hundreds of thousands of simultaneous tasks—with direct synchronous control flow, though its primary boundary lies in kernel invisibility: an unintercepted blocking system call or foreign-function (FFI) call can pin and stall its carrier OS thread unless the runtime offloads it to a separate worker thread.

    Common Forms

    • N:1 (Many-to-One) Green Threads: Multiplex all user-space threads onto a single operating system thread, keeping memory overhead and runtime locking minimal while restricting execution to one CPU core and risking a full-process stall if any unintercepted blocking syscall occurs (as in early Java 1.1 green threads or Python gevent greenlets).

    • M:N (Many-to-Many) Hybrid Green Threads: Schedule large numbers of green threads across a smaller pool of OS carrier threads—typically using per-worker run queues and work stealing—to combine lightweight user-space switching with true multi-core parallelism (as in Go goroutines, Erlang BEAM processes, and Java virtual threads).

    • Cooperative Green Threads: Transfer control only at explicit yield points, runtime channel operations, or non-blocking I/O boundaries, eliminating involuntary preemption hazards inside runtime code at the risk of starving peer tasks if a thread runs a tight CPU-bound loop.

    • Runtime-Preemptive Green Threads: Augment cooperative scheduling with compiler-inserted safe-point checks at function prologues or loop backedges, or with periodic OS signals, allowing the runtime scheduler to interrupt long-running compute tasks so latency stays bounded across runnable green threads.

    Example

    A Go microservice listening on HTTPS port 8443 handles 40,000 concurrent client connections by spawning a green thread (goroutine) per connection on an 8-core Linux host configured with GOMAXPROCS=8. When a handler calls db.QueryContext() to fetch rows from PostgreSQL and waits for socket data, the runtime registers the socket with epoll, parks the goroutine's lightweight user-space stack, and dispatches another queued goroutine on the same OS carrier thread without a kernel context switch.