# threads API reference

> The `threads` package, from threads 0.6.1, as published on mojoshelf (commit f45e780a8de7), by `mojo doc` from Mojo 1.2.0.

## Modules

- [threads.atomic](https://magmalake.dev/s/threads/api/atomic.md): Atomics that compile on Mojo 1.2.0 *and* on current nightly.
- [threads.ffi](https://magmalake.dev/s/threads/api/ffi.md): Shared FFI vocabulary: the opaque pointer type every `threads` entry point speaks, and the C NULL convention.
- [threads.mutex](https://magmalake.dev/s/threads/api/mutex.md): A `pthread_mutex_t` (and `pthread_cond_t`) you can share between threads.
- [threads.parallel](https://magmalake.dev/s/threads/api/parallel.md): `parallel_for` — the reason this tin exists — plus the `spawn_n` / `join_all` layer underneath it.
- [threads.pool](https://magmalake.dev/s/threads/api/pool.md): `WorkerPool` — N long-lived threads that run until you tell them to stop.
- [threads.thread](https://magmalake.dev/s/threads/api/thread.md): OS threads: `ThreadHandle`, `num_cpus`, `current_thread_id`, CPU pinning.

Minimal pthreads for Mojo — the `threads` package of threads.mojo.

Spawn and join OS threads, share state through atomics and a mutex, and fan a
loop out over cores with `parallel_for`. No dependencies beyond the Mojo
standard library and the pthread symbols every libc already exports.

```mojo
from threads import parallel_for, num_cpus, AtomicCounter
```

Start with `threads.parallel` for the `parallel_for` contract, `threads.pool`
for long-lived workers that run until stopped, `threads.thread` for the thread
and send-contract rules, and `threads.atomic` for why this tin does not simply
re-export `std.atomic`.
