---
name: "Domain"
section: "3o"
platform: "linux"
package: "ocaml"
version: "5.5.0-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/Domain"
---

# Domain(3o)

Domains.

## NAME

Domain - Domains.

## Module

Module   Domain

## Documentation

Module **Domain**\
  :  **sig end**

Domains.

See 'Parallel programming' chapter in the manual.

**Since** 5.0

**Alert unstable.** The Domain interface may change in incompatible ways in the future.

*type* **!'a** *t*

A domain of type  **'a t** runs independently, eventually producing a result of type 'a, or an exception

*val spawn* :  **(unit -\> 'a) -\> 'a t**

**spawn f** creates a new domain that runs in parallel with the current domain.

**Raises Failure** if the program has insufficient resources to create another domain.

*val join* :  **'a t -\> 'a**

**join d** blocks until domain  **d** runs to completion. If  **d** results in a value, then that is returned by  **join d** . If  **d** raises an uncaught exception, then that is re-raised by  **join d** .

If the domain has created any systhreads (via  **Thread.create** ), then it tries to join those systhreads (same as calling  **Thread.join** ) before returning.

*type id* = private  **int**

Domains have unique integer identifiers

*val get\_id* :  **'a t -\> id**

**get\_id d** returns the identifier of the domain  **d**

*val self* :  **unit -\> id**

**self ()** is the identifier of the currently running domain

*val before\_first\_spawn* :  **(unit -\> unit) -\> unit**

**before\_first\_spawn f** registers  **f** to be called before the first domain is spawned by the program. The functions registered with **before\_first\_spawn** are called on the main (initial) domain. The functions registered with  **before\_first\_spawn** are called in 'first in, first out' order: the oldest function added with  **before\_first\_spawn** is called first.

**Raises Invalid\_argument** if the program has already spawned a domain.

*val at\_exit* :  **(unit -\> unit) -\> unit**

**at\_exit f** registers  **f** to be called when the current domain exits. Note that  **at\_exit** callbacks are domain-local and only apply to the calling domain. The registered functions are called in 'last in, first out' order: the function most recently added with  **at\_exit** is called first. An example:

```
let temp_file_key = Domain.DLS.new_key (fun _ ->
  let tmp = snd (Filename.open_temp_file "" "") in
  Domain.at_exit (fun () -> close_out_noerr tmp);
  tmp)

```

The snippet above creates a key that when retrieved for the first time will open a temporary file and register an  **at\_exit** callback to close it, thus guaranteeing the descriptor is not leaked in case the current domain exits.

*val cpu\_relax* :  **unit -\> unit**

If busy-waiting, calling cpu\_relax () between iterations will improve performance on some CPU architectures

*val is\_main\_domain* :  **unit -\> bool**

**is\_main\_domain ()** returns true if called from the initial domain.

*val count* :  **unit -\> int**

The number of domains that have been spawned (including the initial domain) and have not yet terminated.

**Since** 5.5

*val recommended\_domain\_count* :  **unit -\> int**

The recommended maximum number of domains which should be running simultaneously (including domains already running).

The value returned is at least  **1** .

*val self\_index* :  **unit -\> int**

The index of the current domain. It is an integer unique among currently-running domains, in the interval  **0; N-1** where N is the peak number of domains running simultaneously so far.

The index of a terminated domain may be reused for a new domain. Use  **(Domain.self () :\> int)** instead for an identifier unique among all domains ever created by the program.

**Since** 5.3

*module DLS :* **sig end**
