std/pthread

March 26, 2026 ยท View on GitHub

POSIX threads (pthreads) interface for multithreading.

Types

pthread::attribute

Thread attributes structure (platform-specific size).

pthread::mutex

Mutex structure for thread synchronization (platform-specific size).

Functions

create

int create(ulong* thread, pthread::attribute* attr, char*(char*) start, char* arg)

Creates new thread. Returns 0 on success.

Parameters:

  • thread - Output thread ID
  • attr - Thread attributes (null for defaults)
  • start - Thread function
  • arg - Argument passed to thread function

join

int join(ulong thread, char** valuePtr)

Waits for thread to terminate. Returns 0 on success.

self

ulong self()

Returns calling thread's ID.

exit

void exit(char* retVal)

Terminates calling thread with return value.

cancel

int cancel(ulong thread)

Requests thread cancellation. Returns 0 on success.

detach

int detach(ulong thread)

Detaches thread (resources freed on termination). Returns 0 on success.

Example

import <std/pthread>

char* threadFunc(char* arg) {
    int* num = cast(int*)arg;
    std::print("Thread received: ", num[0], "\n");
    pthread::exit(null);
}

int value = 42;
ulong threadId;
pthread::create(&threadId, null, threadFunc, &value);
pthread::join(threadId, null);