User-level thread

Also known as: user thread

On this page 8

A user-level thread (also called a user thread) is a thread of execution that is created, scheduled and switched by a library or language runtime in user space, while the kernel sees only the process or kernel thread that hosts it. In the study of processes & scheduling in operating systems, user-level threads are the counterpart of the kernel thread: they trade kernel visibility for very cheap creation and context switches.

Green threads in early Java, Go goroutines, Erlang processes, fibers and many coroutine schedulers are practical forms of the idea. Their design decides how a program behaves on blocking system calls, on multiple CPU cores and under heavy concurrency.

How a user-level thread runs without the kernel

A user-level thread runs because a user-space scheduler decides which thread uses the host kernel thread at each moment. The runtime keeps a thread control block for every user thread, holding its saved registers, stack pointer, program counter, state and priority, much as the kernel keeps a process control block for each process.

Switching between two user threads is an ordinary function call into the runtime: it saves the callee-saved registers, swaps the stack pointer and jumps to the other thread. No mode switch into the kernel occurs, no system call is made and the TLB and page tables stay untouched. A switch therefore costs tens of nanoseconds, compared with roughly one to several microseconds for a kernel-mediated thread switch, depending on the CPU and mitigations in effect.

The components below make up a typical user-level threading runtime.

ComponentRoleTypical implementation
Thread control blockStores context and state of one user threadStruct in the runtime heap
Private stackHolds local variables and call framesFixed 64 KiB to 1 MiB, or growable segments
Ready queueLists threads waiting to runFIFO, priority list or work-stealing deque
Context switch routineSaves and restores registersShort assembly or swapcontext-style code
Scheduler loopPicks the next thread to runRuns on each host kernel thread
Components of a user-level threading runtime

Threading models that map user threads to kernel threads

The threading model defines how many user-level threads share how many kernel threads, and it determines both performance and failure behavior. Three classic mappings exist, and modern runtimes mostly use the hybrid one.

ModelMappingParallelism on multicoreBehavior on blocking call
Many-to-oneAll user threads on one kernel threadNoneWhole process stalls
One-to-oneEach user thread backed by a kernel threadFullOnly the caller blocks
Many-to-many (M:N)M user threads over N kernel threadsUp to N coresRuntime hands work to another kernel thread
Mapping models for user-level threads

Pure many-to-one libraries, such as early GNU Pth or the original Java green threads on Solaris, are user-level threads in the strictest sense. One-to-one libraries such as NPTL on Linux are named user threads in some textbooks, but every thread is in fact a kernel-scheduled entity. The M:N model used by Go, Erlang and Java virtual threads keeps user-level scheduling while still exploiting several cores.

User-level thread compared with kernel thread and process

A user-level thread differs from a kernel thread mainly in who schedules it and what the kernel can see. The comparison below shows the practical consequences, including how each relates to a full process.

PropertyUser-level threadKernel threadProcess
Scheduled byUser-space runtimeKernel schedulerKernel scheduler
Creation costAbout 1 microsecond or lessAbout 10 to 50 microsecondsHundreds of microseconds
Switch costTens of nanosecondsAbout 1 to 5 microsecondsSeveral microseconds plus TLB effects
Memory per unitA few KiB to 1 MiBKernel stack plus user stackSeparate address space
Kernel visibilityInvisibleVisibleVisible
IsolationShares address spaceShares address spaceSeparate address space
User-level thread, kernel thread and process side by side

The figures are orders of magnitude, not fixed values; they vary with hardware, kernel version, stack size and security mitigations. The pattern, however, is stable: user-level threads win on creation and switching speed, while kernel threads win on true parallelism and transparent blocking.

Scheduling user-level threads inside the runtime

Scheduling of user-level threads is controlled by the application runtime, which lets it apply application-specific policies that the general-purpose kernel scheduler cannot know. A web server runtime may prioritize threads close to finishing a request, while a simulation may run threads in strict round-robin order.

Most runtimes rely on cooperative multitasking: a thread runs until it yields, blocks on a runtime primitive or finishes. Preemption is harder because the kernel delivers timer interrupts to kernel threads, not to user threads. Some runtimes emulate it with signal-based preemption; Go, for example, sends asynchronous signals to stop goroutines that run tight loops for more than about 10 milliseconds.

  • Yield points are inserted at channel operations, lock waits, I/O calls and sometimes function prologues.
  • Work stealing lets an idle kernel thread take ready user threads from a busy one, balancing load across cores.
  • Time slices in user space are approximate, because the runtime cannot interrupt a thread at an arbitrary instruction without kernel help.

Blocking calls and other limitations

The central weakness of user-level threads is that a blocking system call blocks the host kernel thread. In a many-to-one library, one thread reading a slow file stops every other thread in the process, because the kernel suspends the only schedulable entity it knows about.

Runtimes counter this with non-blocking I/O and event notification through epoll, kqueue or I/O completion ports, wrapping system calls so that a thread parks itself instead of blocking the kernel thread. Historic research proposed scheduler activations, an upcall mechanism through which the kernel informs the runtime when a kernel thread blocks, but mainstream kernels did not adopt it widely.

The table summarizes recurring problems, their causes and the usual remedies.

SymptomCauseRemedy
Entire program freezesBlocking system call in many-to-one modelUse non-blocking I/O or an M:N runtime
One core saturated, others idleNo mapping to multiple kernel threadsAdopt work-stealing M:N scheduling
Thread starves its peersLong loop without yield pointInsert yields or enable runtime preemption
Kernel thread pinnedNative code or locks held during blockingMove blocking work to a dedicated pool
Confusing profiler outputTools see only kernel threadsUse runtime-aware profilers and tracers
Common problems with user-level threads

Priority inversion can also cross the boundary: a high-priority user thread may wait on a lock held by a user thread whose host kernel thread the operating system has descheduled. Debuggers and thread-local storage designed for kernel threads may likewise behave unexpectedly.

When user-level threads are the right choice

User-level threads are the right choice when a program must handle very large numbers of concurrent, mostly waiting tasks, such as network connections or message handlers. They are a poor fit for CPU-bound numerical work that needs exactly one thread per core.

  • Choose an M:N runtime for servers that hold 100,000 or more concurrent connections.
  • Choose kernel threads for CPU-heavy parallel computation and for code that calls blocking native libraries.
  • Avoid pure many-to-one libraries in new code unless the platform lacks kernel thread support.

Stripped to the essentials

A computer runs many small jobs at once by switching between them very quickly. Usually the operating system does that switching. With user-level threads, the program does the switching itself, without asking the operating system.

Think of a restaurant where the manager assigns one cook to a kitchen, and that cook juggles twenty orders on his own. The manager sees one cook, not twenty orders. Juggling is fast because nobody has to ask the manager. But if the cook leaves to fetch ingredients, every order waits. The main takeaway: user-level threads are cheap and fast, but one blocked thread can stall the others unless the runtime is designed to avoid it.

Where it is applied

A team runs a chat backend that keeps about 200,000 open WebSocket connections on a single server with 16 cores. Using one kernel thread per connection would need 200,000 kernel threads, each with its own kernel stack and scheduler entry, which exhausts memory and slows scheduling.

The team writes the service in Go. Each connection becomes a goroutine that starts with a stack of about 2 KiB, and the runtime multiplexes them over 16 kernel threads. When a goroutine waits for a message, the runtime parks it and registers the socket with epoll, so the kernel thread immediately runs another goroutine.

The outcome is roughly 1 GiB of total stack memory instead of many gigabytes, steady latency under load and full use of all 16 cores. The team still moves one blocking legacy library call to a small dedicated worker pool, because it would otherwise pin kernel threads.