[ blog » 2026 ]Easy Concurrency in Ocaml 5 (by Philipp Gesang, location: Munich)
2026-12-02

For many years, Multicore Ocaml was pretty high on the vaporware charts. In fact it took so long for Ocaml 5 to ship that when it happened back in 2022 I had long lost track of it and didn’t bother to educate myself on what the final result looks like to the user. Turns out this was a mistake, as the result is rather polished and strikingly easy to use (much more so than earlier approaches to concurrency like lwt).

What usecase could I possibly have that would benefit from multithreading? Interestingly enough it’s the static site generator that powers this blog which over the years has become a bottleneck. The markdown to HTML conversion itself (leveraging the excellent Omd library) is still blazing fast. However, generating thumbnails from images with CamlImages may take several seconds on the tiny Hetzner CAX11 instance the blog is served from. Especially when posting articles that delay can test my patience as I’m usually tired from a day on the bike when I do that.

Recently during a long overdue server maintenance I realized this part could benefit from utilizing all four cores of this ARM VPS. It didn’t originally, because back when I wrote most of the blog code Ocaml was still all single core. It’s never too late, I suppose.

Practical Use

To

Domains

The approach we take in multicore OCaml is to avoid as much as possible of this shared state. First, to reduce state shared between mutators and GC, we do not use an explicit gray colour. Instead, each domain maintains its own stack of gray objects, and when the write barrier needs to gray an object it marks it and adds it to the local domain’s gray stack. Since GC and OCaml code are interleaved on a single domain, no synchronisation is necessary. (This scheme does mean that it’s possible for an object to be on the mark stack of two separate domains. See below).