Implementation:Google deepmind Mujoco Thread Pool
| Knowledge Sources | |
|---|---|
| Domains | Concurrency, Threading, Task Scheduling |
| Last Updated | 2026-02-15 04:00 GMT |
Overview
C++ implementation of MuJoCo's thread pool providing multi-threaded task execution with per-thread stack sharding and a lockless task queue.
Description
This file implements ThreadPoolImpl, the concrete C++ class behind the opaque mjThreadPool C struct. It manages a pool of worker threads that consume tasks from a lockless queue. Each worker thread is assigned a unique ID (0 for main, 1..N for workers), and the pool supports binding to mjData to configure multi-threaded stack memory sharding. The stack sharding splits the arena into per-thread shards so each worker can allocate temporary memory without contention. The implementation also provides an allocation mutex for thread-safe arena allocations.
Usage
Created via mju_threadPoolCreate and bound to mjData via mju_bindThreadPool to enable parallel physics stepping. Tasks are enqueued with mju_threadPoolEnqueue and executed by available worker threads.
Code Reference
Source Location
- Repository: Google_deepmind_Mujoco
- File: src/thread/thread_pool.cc
- Lines: 1-312
Key Functions
// Create a thread pool with the specified number of worker threads
mjThreadPool* mju_threadPoolCreate(size_t number_of_threads);
// Bind a thread pool to mjData and configure multi-threaded stack shards
void mju_bindThreadPool(mjData* d, void* thread_pool);
// Get the number of running threads in the pool
size_t mju_threadPoolNumberOfThreads(mjThreadPool* thread_pool);
// Get the ID of the current worker thread
size_t mju_threadPoolCurrentWorkerId(mjThreadPool* thread_pool);
// Enqueue a task for execution by a worker thread
void mju_threadPoolEnqueue(mjThreadPool* thread_pool, mjTask* task);
// Lock/unlock the arena allocation mutex
void mju_threadPoolLockAllocMutex(mjThreadPool* thread_pool);
void mju_threadPoolUnlockAllocMutex(mjThreadPool* thread_pool);
// Destroy the thread pool and join all worker threads
void mju_threadPoolDestroy(mjThreadPool* thread_pool);
// Get the destructive interference (cache line) size
size_t mju_getDestructiveInterferenceSize(void);
Import
#include "thread/thread_pool.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| number_of_threads | size_t | Yes | Number of worker threads to create (capped at mjMAXTHREAD) |
| d | mjData* | Yes (for bind) | Simulation data whose arena will be sharded for multi-threaded use |
| task | mjTask* | Yes (for enqueue) | Task structure with function pointer and arguments to execute |
Outputs
| Name | Type | Description |
|---|---|---|
| mjThreadPool* | pointer | Opaque handle to the created thread pool |
| d->threadpool | void* | The bound thread pool pointer stored in mjData |
| Stack shards | mjStackInfo | Per-thread stack memory regions configured in mjData arena |