Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Google deepmind Mujoco MjThread Header

From Leeroopedia
Knowledge Sources
Domains C API, Threading, Concurrency, Task Parallelism
Last Updated 2026-02-15 04:00 GMT

Overview

Compact C header file that defines the MuJoCo thread pool and task execution API for parallel simulation computations.

Description

mjthread.h defines the threading primitives for MuJoCo's parallel execution model in just 42 lines. It declares the mjMAXTHREAD constant (128 maximum threads), the mjtTaskStatus enum (NEW, QUEUED, COMPLETED), the mjfTask function pointer type (void* -> void*), the opaque mjThreadPool structure (containing only nworker), and the mjTask structure (containing a function pointer, argument pointer, and volatile status field). This minimal API enables MuJoCo to distribute independent simulation computations (such as island-based constraint solving) across a thread pool.

Usage

This header is included when using MuJoCo's multi-threaded simulation features. A thread pool is created with a specified number of workers, tasks are submitted with function pointers and arguments, and the status field is polled or waited on for completion. This is used internally by MuJoCo for parallelizing constraint island solving and other independent computations.

Code Reference

Source Location

Key Functions

#define mjMAXTHREAD 128        // maximum number of threads in a thread pool

typedef enum mjtTaskStatus_ {  // status values for mjTask
  mjTASK_NEW = 0,              // newly created
  mjTASK_QUEUED,               // enqueued in a thread pool
  mjTASK_COMPLETED             // completed execution
} mjtTaskStatus;

typedef void* (*mjfTask)(void*);  // function pointer type for mjTask

struct mjThreadPool_ {
  int nworker;  // number of workers in the pool
};
typedef struct mjThreadPool_ mjThreadPool;

struct mjTask_ {
  mjfTask func;         // pointer to the function that implements the task
  void* args;           // arguments to func
  volatile int status;  // status of the task
};
typedef struct mjTask_ mjTask;

Import

#include <mujoco/mjthread.h>

I/O Contract

Inputs

Name Type Required Description
func mjfTask (void* -> void*) Yes Function pointer implementing the task logic
args void* Yes Opaque argument pointer passed to the task function
nworker int Yes Number of worker threads in the pool (max 128)

Outputs

Name Type Description
status volatile int (mjtTaskStatus) Task execution status: NEW (0), QUEUED (1), or COMPLETED (2)
return value void* Return value from the task function

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment