18.1 threaded-r
threaded-r is a fork of GNU R 4.6.1 that implements the apartment model: several isolated interpreters run in one process, each owned by one thread. Each interpreter keeps its own object state, and interpreters exchange values only through channels, which serialize them. GNU R assumes one interpreter per process, so most of the work is finding the places where that assumption is built in.
Each interpreter owns its heap, symbol table, string cache and environment chain. No SEXP migrates between interpreters. C code creates and drives interpreters through a public embedding API: R_NewInterpreter, R_DestroyInterpreter, R_InterpreterSpawn, Join and Detach, and the channel calls R_ChannelCreate, Send, Recv and Close. Partitioning state by owner is meant to remove most races by construction, without locks; where state has to stay shared, such as the graphics device table, the fork takes a lock. In practice every global still outside the partition is a race waiting to be found: the residual crash described below came from nineteen process-wide scratch buffers in upstream R.
The work started with a census of global mutable state, which counted 1,574 mutable globals in upstream R. The 35 that belong to a thread live in R_ThreadState, which had grown to 44 members by patch 0141 (2026-08-24), counting fields added for the Windows port. Those that belong to an interpreter move into R_InterpreterState, which had 556 members after patch 0127 (2026-08-22), the count in the generated header RInterpState.h. An immortal arena holds the eleven shared constants, such as R_NilValue and NA_STRING, plus R_EmptyEnv and the 38 shared symbols with their print names, that every interpreter must see at the same address (compiled packages compare against most of them). Moving base-function bindings out of each symbol’s SYMVALUE slot into a hashed base environment measured 1.3 to 1.9 percent slower on a single-threaded microbenchmark built to make it look as bad as possible (the measurement). The fork then took a narrower route: each interpreter has its own symbol table, so ordinary base bindings stay in their own symbols’ SYMVALUE slots, and only the 38 symbols that interpreters share keep their binding in a per-interpreter record (docs/ledger.md).
makeCluster(type = "THREAD") creates workers as threads in one process, each with its own interpreter, instead of separate operating system processes. It implements the snow interface that parallel uses: clusterCall, clusterEvalQ, clusterExport, parLapply, parSapply, clusterMap, parLapplyLB, and worker error propagation, each checked against a PSOCK cluster with identical().
18.1.1 Usage
library(parallel)
cl <- makeCluster(4, type = "THREAD")
clusterSetRNGStream(cl, 42)
result <- parLapply(cl, 1:100, function(i) {
mean(rnorm(1e4))
})
stopCluster(cl)The four workers are threads in one process, each owning its own interpreter. Only the type argument differs from a PSOCK cluster in this script. A package with compiled code should be loaded on the main interpreter before the cluster starts, or the first worker to load it claims its native code and every other interpreter is refused. Packages with compiled code do have to be reinstalled for the fork: its ABI tag refuses a binary built for stock R instead of letting it corrupt memory. A worker can load a package’s compiled code only if the package has declared itself capable; the native code of any other package stays with the interpreter that loaded it first, normally the main one.
18.1.2 Measurements
My own demo, tools/bench/thread-vs-psock.R and tools/demo/thread-cluster-demo.R at N=2, reported 1.8x faster startup, 2.1x less memory, and identical results against PSOCK at patch 0092 (2026-08-14). Both run base-only workloads, on WSL2. N=2 is small, and this is my own measurement, not an independent replication.
clusterSetRNGStream gives bit-for-bit the same streams as on PSOCK. The capability corpus runs every installed base and recommended package against a fresh THREAD worker. On 2026-08-16 three of the twenty-nine were declared capable. By 2026-08-20, at 121 patches, the corpus accounted for all twenty-nine: twenty-two packages with compiled code declared capable, Matrix among them, six pure-R packages loading on a worker, and tcltk kept on the main interpreter by decision, because it drives one process-wide Tcl interpreter (docs/r2/go-no-go.md).
18.1.3 Open work
The residual crash left open at the end of P7 is root-caused and fixed. A single uncycled concurrent library("tools") across THREAD workers used to hit it 90 to 95 percent of the time, and patch 0101’s process-wide namespace lock only cut that to roughly 25 to 45 percent. The R1 phase traced it to nineteen process-wide scratch buffers in upstream R, one of them a cross-interpreter use-after-free, and moved them into per-interpreter state (patches 0102 to 0104). With the 0101 lock compiled out, two 50-round campaigns ran clean (docs/r1/go-no-go.md, 2026-08-16), and the conformance suite later passed twice on the thread lane at 121 patches. The lock stays in the series as a second line of defense.
ThreadSanitizer still cannot build threaded-r on its own: TSan crashes in its own shadow-stack bookkeeping while the base packages install. For the R3 release candidate (2026-08-26), a spliced build ran a live THREAD-cluster workload under TSan for the first time, and the 946 reports it raised were worked down to zero on two runs (docs/r3/compatibility-contract.md). That covers the workload exercised, not the whole fork.
Locale is process-global, because setlocale is and R never calls uselocale. The graphics device table is shared behind a lock. Interactive graphics (X11()) and tcltk stay on the main interpreter, each because it runs a process-wide event loop.
18.1.4 Lessons
Most of threaded-r’s work has been finding state. The census counted 1,574 mutable globals, and several of the crashes that held up a phase came from globals the census had already listed and nobody had moved yet. The one storage design it measured for base bindings, a hashed base environment, cost 1.3 to 1.9 percent where the cost should be largest, and the fork later avoided it by giving each interpreter its own symbol table.
18.1.5 Repository
The threaded-r repository is not public yet.
Further reading
- R Core Team. “R Internals”. https://cran.r-project.org/doc/manuals/r-release/R-ints.html
- R Core Team. “Writing R Extensions”. https://cran.r-project.org/doc/manuals/r-release/R-exts.html