Tins · 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. Source.

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.

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.

Modules

  • threads.atomic Atomics that compile on Mojo 1.2.0 and on current nightly. 2 structs · 7 functions
  • threads.ffi Shared FFI vocabulary: the opaque pointer type every threads entry point speaks, and the C NULL convention. 2 functions
  • threads.mutex A pthread_mutex_t (and pthread_cond_t) you can share between threads. 4 structs
  • threads.parallel parallel_for — the reason this tin exists — plus the spawn_n / join_all layer underneath it. 1 struct · 3 functions
  • threads.pool WorkerPool — N long-lived threads that run until you tell them to stop. 2 structs
  • threads.thread OS threads: ThreadHandle, num_cpus, current_thread_id, CPU pinning. 1 struct · 4 functions