> For the complete documentation index, see [llms.txt](https://0x29a.gitbook.io/0x29a-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://0x29a.gitbook.io/0x29a-docs/runtime/thread.md).

# thread

**runtime · global: `thread`**

Real native threads, each with its **own Lua state** (created via `Engine::MakeState`) — they never share the console's state or another script's, so there's no need for manual locking around pure-Lua logic inside the thread.

### `thread.create(code) -> id | nil`

Compiles and runs the string `code` as a Lua chunk, on a new thread. Returns a numeric `id`, or `nil` if the thread couldn't be created.

```lua
local id = thread.create([[
    while not thread.should_stop() do
        -- heavy work that can't block the render frame
        thread.sleep(50)
    end
]])
```

### `thread.sleep(ms)`

Blocks the current thread for `ms` milliseconds. Only makes sense inside a thread created by `thread.create` — never call this on the main state/console (it freezes the whole overlay).

### `thread.should_stop() -> bool`

`true` once the current thread has been signaled to stop (via `thread.stop`). Your loop should check this periodically to exit cleanly.

### `thread.id() -> integer`

The current thread's ID, or `0` when called from the main state/console.

### `thread.stop(id) -> bool`

Signals thread `id` to stop and waits up to 3s for it to finish before releasing its resources. Calling it with the current thread's own `id` just signals (avoids a self-join deadlock).

### `thread.stop_all()`

Stops and releases every active thread.

### `thread.list() -> [id]`

IDs of every currently running thread.

## Clean shutdown

If the thread's chunk defines a global function `__neuro_on_stop`, it's called automatically **before** the thread's Lua state is closed — ideal for freeing native buffers allocated with `mem.alloc` or removing hooks installed only by that thread.

```lua
thread.create([[
    local buf = mem.alloc(256)

    function __neuro_on_stop()
        mem.free(buf)
    end

    while not thread.should_stop() do
        thread.sleep(100)
    end
]])
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://0x29a.gitbook.io/0x29a-docs/runtime/thread.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
