Implementation:Google deepmind Mujoco mju threadPoolCreate
| Knowledge Sources | |
|---|---|
| Domains | Multithreading, Performance |
| Last Updated | 2026-02-15 06:00 GMT |
Overview
Concrete tools for creating and binding thread pools to MuJoCo simulation data provided by the MuJoCo threading API.
Description
mju_threadPoolCreate creates a thread pool with the specified number of worker threads. mju_bindThreadPool binds the pool to an mjData instance, partitioning its arena memory into per-thread shards for parallel computation. Once bound, mj_step automatically uses multi-threaded constraint solving.
Usage
Create a thread pool once at startup, then bind to each mjData before simulation. The thread count should typically match the number of available CPU cores minus one (for the main thread).
Code Reference
Source Location
- Repository: mujoco
- File: src/thread/thread_pool.cc
- Lines (mju_threadPoolCreate): 163-165
- Lines (mju_bindThreadPool): 260-268
Signature
mjThreadPool* mju_threadPoolCreate(size_t number_of_threads);
void mju_bindThreadPool(mjData* d, void* thread_pool);
Import
#include <mujoco/mujoco.h>
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| number_of_threads | size_t | Yes | Number of worker threads |
| d | mjData* | Yes | Data to bind (for mju_bindThreadPool) |
| thread_pool | void* | Yes | Thread pool handle (for mju_bindThreadPool) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | mjThreadPool* | Opaque thread pool handle |
| d (modified) | mjData* | Data bound to thread pool with partitioned arena |
Usage Examples
#include <mujoco/mujoco.h>
mjModel* m = mj_loadXML("scene.xml", NULL, error, 1000);
mjData* d = mj_makeData(m);
// Create thread pool with 4 workers
mjThreadPool* pool = mju_threadPoolCreate(4);
// Bind to data (partitions arena memory)
mju_bindThreadPool(d, pool);
// Simulation now uses multi-threaded solving
for (int i = 0; i < 10000; i++) {
mj_step(m, d);
}
// Cleanup
mj_deleteData(d);
mj_deleteModel(m);
// Note: thread pool lifetime managed separately