PTHREAD(3)
π PTHREAD(3) β POSIX thread functions, but make it make sense π
Okay bestie, this is the PTHREAD(3) page from the Library Functions Manual, and itβs all about the POSIX Threads Library (libpthread, -lpthread). This is part 1, so buckle up. π
πΈ NAME πΈ
pthread β POSIX thread functions. Thatβs the name, bestie. Periodt. π
π LIBRARY π
POSIX Threads Library (libpthread, -lpthread) β yes, thatβs the one you link with. β¨
π§ SYNOPSIS π§
#include <pthread.h>
β¨ DESCRIPTION β¨
Okay so POSIX threads are a set of functions that support applications with requirements for multiple flows of control, called threads, within a process. Multithreading is used to improve the performance of a program. Thatβs the whole vibe: multiple little flows of control doing their thing inside one process, and multithreading can make your program perform better. π¦
The POSIX thread functions are summarized in this section in the following groups:
- Thread Routines
- Attribute Object Routines
- Mutex Routines
- Condition Variable Routines
- Read/Write Lock Routines
- Per-Thread Context Routines
- Cleanup Routines
FreeBSD extensions to the POSIX thread functions are summarized in pthread_np(3). Go check that one if you need the FreeBSD-specific extras, iykyk. π
π§΅ Thread Routines π§΅
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)β Creates a new thread of execution. New main character just dropped. β¨int pthread_cancel(pthread_t thread)β Cancels execution of a thread. Sorry bestie, itβs giving cancel culture. πint pthread_detach(pthread_t thread)β Marks a thread for deletion. Not me setting it free. π¦int pthread_equal(pthread_t t1, pthread_t t2)β Compares two thread IDs. Are they the same? Letβs find out. πvoid pthread_exit(void *value_ptr)β Terminates the calling thread. Bye from the thread itself. πint pthread_join(pthread_t thread, void **value_ptr)β Causes the calling thread to wait for the termination of the specified thread. Itβs literally waiting for its friend to finish. π«Άint pthread_kill(pthread_t thread, int sig)β Delivers a signal to a specified thread. A little signal delivery, very demure. πint pthread_once(pthread_once_t *once_control, void (*init_routine)(void))β Calls an initialization routine once. Once. Not twice. Once. πpthread_t pthread_self(void)β Returns the thread ID of the calling thread. Hi, itβs me. πint pthread_setcancelstate(int state, int *oldstate)β Sets the current threadβs cancelability state. Setting the vibe for cancellation. β¨int pthread_setcanceltype(int type, int *oldtype)β Sets the current threadβs cancelability type. The type of cancellation energy, if you will. πvoid pthread_testcancel(void)β Creates a cancellation point in the calling thread. A little checkpoint, bestie. πvoid pthread_yield(void)β Allows the scheduler to run another thread instead of the current one. Sharing is caring, fr fr. π
πͺ Attribute Object Routines πͺ
int pthread_attr_destroy(pthread_attr_t *attr)β Destroy a thread attributes object. Cleanup era. π§Ήint pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inheritsched)β Get the inherit scheduling attribute from a thread attributes object. Reading the scheduling tea. π΅int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)β Get the scheduling parameter attribute from a thread attributes object. Parameter peek. πint pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)β Get the scheduling policy attribute from a thread attributes object. Policy check. πint pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope)β Get the contention scope attribute from a thread attributes object. Scope it out. πint pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)β Get the stack size attribute from a thread attributes object. How big is the stack? Asking for a friend. πint pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)β Get the stack address attribute from a thread attributes object. Address acquired. π¬int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)β Get the detach state attribute from a thread attributes object. Detach state check, no cap. πint pthread_attr_init(pthread_attr_t *attr)β Initialize a thread attributes object with default values. Fresh start, default settings. β¨int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched)β Set the inherit scheduling attribute in a thread attributes object. Setting the inheritance vibe. π§¬int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)β Set the scheduling parameter attribute in a thread attributes object. Parameter, but make it set. πint pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)β Set the scheduling policy attribute in a thread attributes object. Policy is now set. πint pthread_attr_setscope(pthread_attr_t *attr, int contentionscope)β Set the contention scope attribute in a thread attributes object. Scope: set. πint pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)β Set the stack size attribute in a thread attributes object. Stack size, your call. πint pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)β Set the stack address attribute in a thread attributes object. Address, but make it set. π¬int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)β Set the detach state in a thread attributes object. Detach state: handled. π
π Mutex Routines π
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)β Destroy a mutex attributes object. Mutex attributes? Gone. π§Ήint pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *restrict attr, int *restrict ceiling)β Obtain priority ceiling attribute of mutex attribute object. Priority ceiling tea. π΅int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *restrict attr, int *restrict protocol)β Obtain protocol attribute of mutex attribute object. Protocol received. π‘int pthread_mutexattr_gettype(const pthread_mutexattr_t *restrict attr, int *restrict type)β Obtain the mutex type attribute in the specified mutex attributes object. Type check. πint pthread_mutexattr_init(pthread_mutexattr_t *attr)β Initialize a mutex attributes object with default values. Defaults, but for mutex attributes. β¨int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int ceiling)β Set priority ceiling attribute of mutex attribute object. Ceiling: set. πint pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol)β Set protocol attribute of mutex attribute object. Protocol: set. π‘int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)β Set the mutex type attribute that is used when a mutex is created. This is the type that gets used when a mutex is created, bestie. πint pthread_mutex_destroy(pthread_mutex_t *mutex)β Destroy a mutex. Mutex deleted. πint pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)β Initialize a mutex with specified attributes. Mutex debut. β¨int pthread_mutex_lock(pthread_mutex_t *mutex)β Lock a mutex and block until it becomes available. Locked and waiting. πint pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)β Lock a mutex and block until it becomes available or until the timeout expires. Locked, but with a timer. β°int pthread_mutex_trylock(pthread_mutex_t *mutex)β Try to lock a mutex, but do not block if the mutex is locked by another thread, including the current thread. It wonβt block if the mutex is locked by another thread β including the current thread. Yes, even you, bestie. πint pthread_mutex_unlock(pthread_mutex_t *mutex)β Unlock a mutex. Release the lock, release the drama. ποΈ
π«§ Condition Variable Routines π«§
int pthread_condattr_destroy(pthread_condattr_t *attr)β Destroy a condition variable attributes object. Cond attr: destroyed. π§Ήint pthread_condattr_init(pthread_condattr_t *attr)β Initialize a condition variable attributes object with default values. Fresh cond attr, default values. β¨int pthread_cond_broadcast(pthread_cond_t *cond)β Unblock all threads currently blocked on the specified condition variable. Everybody up, itβs a broadcast. π’int pthread_cond_destroy(pthread_cond_t *cond)β Destroy a condition variable. Condition variable: deleted. πint pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)β Initialize a condition variable with specified attributes. Condition variable debut. πint pthread_cond_signal(pthread_cond_t *cond)β Unblock at least one of the threads blocked on the specified condition variable. Wake at least one bestie. π«Άint pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)β Unlock the specified mutex, wait no longer than the specified time for a condition, and then relock the mutex. Unlock, wait with a time limit, relock. The full little routine. β³int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *mutex)β Unlock the specified mutex, wait for a condition, and relock the mutex. Unlock, wait, relock. Periodt. π
π Read/Write Lock Routines π
int pthread_rwlock_destroy(pthread_rwlock_t *lock)β Destroy a read/write lock object. RW lock: destroyed. π§Ήint pthread_rwlock_init(pthread_rwlock_t *lock, const pthread_rwlockattr_t *attr)β Initialize a read/write lock object. RW lock debut. β¨int pthread_rwlock_rdlock(pthread_rwlock_t *lock)β Lock a read/write lock for reading, blocking until the lock can be acquired. Reading lock, and it will wait until it can get it. πint pthread_rwlock_tryrdlock(pthread_rwlock_t *lock)β Attempt to lock a read/write lock for reading, without blocking if the lock is unavailable. Try to read-lock, but no blocking if itβs unavailable. π ββοΈint pthread_rwlock_trywrlock(pthread_rwlock_t *lock)β Attempt to lock a read/write lock for writing, without blocking if the lock is unavailable. Try to write-lock, no blocking if itβs unavailable. βοΈint pthread_rwlock_unlock(pthread_rwlock_t *lock)β Unlock a read/write lock. Unlocked, bestie. πint pthread_rwlock_wrlock(pthread_rwlock_t *lock)β Lock a read/write lock for writing, blocking until the lock can be acquired. Writing lock, and it will wait until it can get it. βοΈint pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)β Destroy a read/write lock attribute object. RW lock attr: destroyed. π§Ήint pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr, int *pshared)β Retrieve the process shared setting for the read/write lock attribute object. Getting the process shared setting. π«±int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)β Initialize a read/write lock attribute object. RW lock attr debut. β¨int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared)β Set the process shared setting for the read/write lock attribute object. Process shared setting: set. π
π§Ί Per-Thread Context Routines π§Ί
int pthread_key_create(pthread_key_t *key, void (*routine)(void *))β Create a thread-specific data key. A key, just for your thread. πint pthread_key_delete(pthread_key_t key)β Delete a thread-specific data key. Key deleted. ποΈvoid * pthread_getspecific(pthread_key_t key)β Get the thread-specific value for the specified key. Getting your thread-specific value. πint pthread_setspecific(pthread_key_t key, const void *value_ptr)β Set the thread-specific value for the specified key. Setting your thread-specific value. π
π§Ό Cleanup Routines π§Ό
int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))β Register fork handlers. Fork handlers: registered. π΄void pthread_cleanup_pop(int execute)β Remove the routine at the top of the calling threadβs cancellation cleanup stack and optionally invoke it. Pops the cleanup routine and can optionally invoke it. π«§void pthread_cleanup_push(void (*routine)(void *), void *routine_arg)β Push the specified cancellation cleanup handler onto the calling threadβs cancellation stack. Pushes a cleanup handler onto the calling threadβs cancellation stack. π§
π IMPLEMENTATION NOTES (the tea β)
Okay so real talk about how this actually works on your machine, bestie:
The current FreeBSD POSIX thread implementation is literally built into the 1:1 Threading Library, aka libthr, aka -lthr when you're linking. Iconic. β¨
It contains thread-safe versions of the Standard C Library (libc, -lc) functions and the thread functions, so you're not out here writing wrappers like a clown. π«Ά
Threaded applications are linked with this library, periodt. So if your app is doing the multithreaded thing, that's the vibe you're linking against. π§
π SEE ALSO (your reading list, bestie π)
Like, go read these next, no cap β they're all (3) manpages and they're all gonna eat:
libthr(3)pthread_atfork(3)pthread_attr(3)pthread_cancel(3)pthread_cleanup_pop(3)pthread_cleanup_push(3)pthread_cond_broadcast(3)pthread_cond_destroy(3)pthread_cond_init(3)pthread_cond_signal(3)pthread_cond_timedwait(3)pthread_cond_wait(3)pthread_condattr_destroy(3)pthread_condattr_init(3)pthread_create(3)pthread_detach(3)pthread_equal(3)pthread_exit(3)pthread_getspecific(3)pthread_join(3)pthread_key_delete(3)pthread_kill(3)pthread_mutex_destroy(3)pthread_mutex_init(3)pthread_mutex_lock(3)pthread_mutex_trylock(3)pthread_mutex_unlock(3)pthread_mutexattr_destroy(3)pthread_mutexattr_getprioceiling(3)pthread_mutexattr_getprotocol(3)pthread_mutexattr_gettype(3)pthread_mutexattr_init(3)pthread_mutexattr_setprioceiling(3)pthread_mutexattr_setprotocol(3)pthread_mutexattr_settype(3)pthread_np(3)pthread_once(3)pthread_rwlock_destroy(3)pthread_rwlock_init(3)pthread_rwlock_rdlock(3)pthread_rwlock_unlock(3)pthread_rwlock_wrlock(3)pthread_rwlockattr_destroy(3)pthread_rwlockattr_getpshared(3)pthread_rwlockattr_init(3)pthread_rwlockattr_setpshared(3)pthread_self(3)pthread_setcancelstate(3)pthread_setcanceltype(3)pthread_setspecific(3)pthread_testcancel(3)
That's the whole squad, fr fr. π
π STANDARDS (the receipts π§Ύ)
So here's how you know who's official and who's just vibing:
The functions with the pthread_ prefix and not the _np suffix or the pthread_rwlock prefix conform to ISO/IEC 9945-1:1996 (βPOSIX.1β). Those are the certified good girls β fully standard, fully iconic. β¨
The functions with the pthread_ prefix and the _np suffix are non-portable extensions to POSIX threads. _np literally stands for "not portable," so likeβ¦ don't be shocked when they don't exist everywhere, babe. π
The functions with the pthread_rwlock prefix are extensions created by The Open Group as part of the Version 2 of the Single UNIX Specification (βSUSv2β). Fancy committee energy, but we respect it. π
π tiny reminder bestie: _np = not portable. If you ship it, that's a choice, and the consequences are yours. Slay responsibly.
And that's the whole page, periodt! π This one's from the Debian crew, dated October 12, 2021, and it's the PTHREAD(3) page β go forth and thread safely, my love. π¦π
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright (c) 1996 John Birrell <jb@cimlogic.com.au>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms