1. Index
  2. Shell
  3. C
  4. POSIX
  5. JavaScript

Multithreading

Ein Ausführungsstrang erlaubt die gleichzeitige Ausführung von Funktionen innerhalb eines Prozesses. Dabei können Ausführungsstränge auch auf mehreren Prozessoren parallel laufen. Jeder Thread bekommt eigene Register und einen eigenen Stack, jedoch teilen sich alle Ausführungsstränge den virtuellen Adressraum und die Dateideskriptoren. Daher muß der Zugriff auf globale Variablen und Ressourcen synchronisiert werden.

threads.h

ANSI-Threads

typedef thrd_t
int thrd_create (thrd_t * thr, thrd_start_t func, void * arg)
thrd_tthrd_current(void)
int thrd_detach (thrd_t thr)
int thrd_equal (thrd_t thr0, thrd_t thr1)
noreturn void thrd_exit ( int res)
int thrd_join (thrd_t thr, int * res)
int thrd_sleep (const struct timespec * duration,
( struct timespec * remaining)
void thrd_yield (void)
typedefmtx_t
void mtx_destroy (mtx_t * mtx)
int mtx_init (mtx_t * mtx, int type)
int mtx_lock (mtx_t * mtx)
int mtx_timedlock (mtx_t * mtx, const struct timespec * timeout)
int mtx_trylock (mtx_t * mtx)
int mtx_unlock (mtx_t * mtx)
typedefcnd_t
void call_once (once_flag * flag, void (* func)(void))
void cnd_broadcast (cnd_t * cond)
void cnd_destroy (cnd_t * cond)
void cnd_init (cnd_t * cond)
void cnd_signal (cnd_t * cond)
void cnd_timedwait (cnd_t * cond, mtx_t * mtx,
const struct timespec * timeout)
void cnd_wait (cnd_t * cond, mtx_t * mtx)
typedeftss_t
int tss_create (tss_t * key, tss_dtor_t dtor)
void tss_delete (tss_t key)
void * tss_get (tss_t key)
int tss_set (tss_t key, void * val)
time_t tv_sec Sekunden
long int tv_nsec Nanosekunden

stdatomic.h

Atomare Operationen sind als Makros implementiert und arbeiten zum Teil mit generischen Typen

typedefatomic_flag
bool atomic_flag_test_and_set (volatile atomic_flag * obj) Setzt ein Bit und liefert den Vorwert
void atomic_flag_clear (volatile atomic_flag * obj) Löscht ein Bit
volatile A
void atomic_init (volatile A * obj, C desired) Initialisiert ein Objekt
bool atomic_is_lock_free (const v. A * obj) Zeigt an, ob ein Objekt lockfrei ist
void atomic_store (volatile A * obj, C desired) Speichert einen Wert in einem Objekt
C atomic_load (volatile A * obj) Liest einen Wert aus einem Objekt
C atomic_exchange (volatile A * obj) Tauscht einen Wert mit dem Wert eines Objekts
bool atomic_compare_exchange_strong (volatile A * obj, C * expected, C desired) Tauscht einen Wert, wenn er den Erwartungen entspricht
bool atomic_compare_exchange_weak (volatile A * obj, C * expected, C desired)
C atomic_fetch_add (volatile A * obj, M arg) Addiert einen Wert
C atomic_fetch_sub (volatile A * obj, M arg) Subtrahiert einen Wert
C atomic_fetch_or (volatile A * obj, M arg) Logisches ODER
C atomic_fetch_xor (volatile A * obj, M arg) Logisches XOR
C atomic_fetch_and (volatile A * obj, M arg) Logisches UND
A kill_dependency ( A y)
typedefmemory_order
void atomic_thread_fence (memory_order order) Generische Trennungsprimitive
void atomic_signal_fence (memory_order order) Trennung zwischen Thread und Signalverarbeitung

Die Funktionen für Flags und Objekte existieren noch in einer _explicit-Variante, die einen zusätzlichen memory_order-Parameter kennt.

memory_order_relaxed
memory_order_consume
memory_order_acquire
memory_order_release
memory_order_acq_rel
memory_order_seq_cst

pthread.h

POSIX-Threads

typedefpthread_t
pthread_tpthread_self(void) Liefert Nummer des aktuellen Threads
int pthread_create (pthread_t * thread, pthread_attr_t * attr, void * (* start_routine) (void *), void * arg)
int pthread_detach (pthread_t thread) Überlässt einen Thread sich selbst
int pthread_join (pthread_t thread, void ** thread_return) Wartet auf Beendigung eines Ausführungsstrags
int pthread_equal (pthread_t thread1, pthread_t thread2) Prüft, ob beide Handles den selben Thread identifizieren
int pthread_yield (void)
void pthread_exit (void * retval) Beendet diesen Thread
int pthread_getconcurrency (void)
int pthread_setconcurrency (int level)
int pthread_setschedprio (pthread_t thread, int prio
int pthread_setschedparam (pthread_t thread, int policy, const struct sched_param * param)
int pthread_getschedparam (pthread_t thread, int * policy, struct sched_param * param)
typedefpthread_attr_t
int pthread_attr_init ( pthread_attr_t * attr)
int pthread_attr_destroy ( pthread_attr_t * attr)
int pthread_attr_setschedparam ( pthread_attr_t * attr, const struct sched_param * param)
int pthread_attr_getschedparam (const pthread_attr_t * attr, struct sched_param * param)
int pthread_attr_setdetachstate ( pthread_attr_t * attr, int detachstate)
int pthread_attr_getdetachstate (const pthread_attr_t * attr, int * detachstate)
int pthread_attr_setschedpolicy ( pthread_attr_t * attr, int policy)
int pthread_attr_getschedpolicy (const pthread_attr_t * attr, int * policy)
int pthread_attr_setinheritsched ( pthread_attr_t * attr, int inherit)
int pthread_attr_getinheritsched (const pthread_attr_t * attr, int * inherit)
int pthread_attr_setscope ( pthread_attr_t * attr, int scope)
int pthread_attr_getscope (const pthread_attr_t * attr, int * scope)
int pthread_attr_setguardsize ( pthread_attr_t * attr, size_t guardsize
int pthread_attr_getguardsize ( pthread_attr_t * attr, size_t * guardsize
int pthread_attr_setstacksize ( pthread_attr_t * attr, size_t stacksize
int pthread_attr_getstacksize ( pthread_attr_t * attr, size_t * stacksize
int pthread_attr_setstack ( pthread_attr_t * attr, void * stackaddr, size_t stacksize
int pthread_attr_getstack ( pthread_attr_t * attr, void ** stackaddr, size_t * stacksize
PTHREAD_CREATE_JOINABLE
PTHREAD_CREATE_DETACHED
SCHED_OTHER
SCHED_RR
SCHED_FIFO
PTHREAD_EXPLICIT_SCHED
PTHREAD_INHERIT_SCHED
PTHREAD_SCOPE_SYSTEM
PTHREAD_SCOPE_PROCESS

Wechselseitiger Ausschluß

pthread_mutex_t PTHREAD_MUTEX_INITIALIZER
pthread_mutex_t PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
pthread_mutex_t PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
typedefpthread_mutex_t
int pthread_mutex_init ( pthread_mutex_t * mutex, const pthread_mutexattr_t * mutexattr)
int pthread_mutex_lock ( pthread_mutex_t * mutex)
int pthread_mutex_trylock ( pthread_mutex_t * mutex)
int pthread_mutex_unlock ( pthread_mutex_t * mutex)
int pthread_mutex_destroy ( pthread_mutex_t * mutex)
int pthread_mutex_consistent ( pthread_mutex_t * mutex
int pthread_mutex_getprioceiling (const pthread_mutex_t * mutex, int * prioceiling
int pthread_mutex_setprioceiling ( pthread_mutex_t * mutex, int prioceiling, int * old_ceiling
int pthread_mutex_timedlock ( pthread_mutex_t * mutex, const struct timespec * abstime
typedefpthread_mutexattr_t
int pthread_mutexattr_init ( pthread_mutexattr_t * attr)
int pthread_mutexattr_destroy ( pthread_mutexattr_t * attr)
int pthread_mutexattr_settype ( pthread_mutexattr_t * attr, int kind)
int pthread_mutexattr_gettype (const pthread_mutexattr_t * attr, int * kind)
int pthread_mutexattr_setpshared ( pthread_mutexattr_t * attr, int pshared)
int pthread_mutexattr_getpshared (const pthread_mutexattr_t * attr, int * pshared)
int pthread_mutexattr_getrobust (const pthread_mutexattr_t * attr, int * robust
int pthread_mutexattr_setrobust ( pthread_mutexattr_t * attr, int robust
typedefpthread_cond_t
#define PTHREAD_COND_INITIALIZER
int pthread_cond_init ( pthread_cond_t * cond, pthread_condattr_t * cond_attr)
int pthread_cond_signal ( pthread_cond_t * cond)
int pthread_cond_broadcast ( pthread_cond_t * cond)
int pthread_cond_wait ( pthread_cond_t * cond, pthread_mutex_t * mutex)
int pthread_cond_timedwait ( pthread_cond_t * cond, pthread_mutex_t * mutex,
const struct timespec * abstime)
int pthread_cond_destroy ( pthread_cond_t * cond)
typedefpthread_condattr_t
int pthread_condattr_init ( pthread_condattr_t * attr)
int pthread_condattr_destroy ( pthread_condattr_t * attr)
int pthread_condattr_getpshared (const pthread_condattr_t * attr, int * pshared)
int pthread_condattr_setpshared ( pthread_condattr_t * attr, int pshared)
int pthread_condattr_getclock (const pthread_condattr_t * attr, clockid_t * clock_id
int pthread_condattr_setclock ( pthread_condattr_t * attr, clockid_t clock_id
typedefpthread_rwlock_t
int pthread_rwlock_init ( pthread_rwlock_t * rwlock,
const pthread_rwlockattr_t * attr)
int pthread_rwlock_destroy ( pthread_rwlock_t * rwlock)
int pthread_rwlock_rdlock ( pthread_rwlock_t * rwlock)
int pthread_rwlock_tryrdlock ( pthread_rwlock_t * rwlock)
int pthread_rwlock_timedrdlock ( pthread_rwlock_t * rwlock,
const struct timespec * abstime)
int pthread_rwlock_wrlock ( pthread_rwlock_t * rwlock)
int pthread_rwlock_trywrlock ( pthread_rwlock_t * rwlock)
int pthread_rwlock_timedwrlock ( pthread_rwlock_t * rwlock,
const struct timespec * abstime)
int pthread_rwlock_unlock ( pthread_rwlock_t * rwlock)
typedefpthread_rwlockattr_t
int pthread_rwlockattr_init ( pthread_rwlockattr_t * attr)
int pthread_rwlockattr_destroy ( pthread_rwlockattr_t * attr)
int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t * attr, int * pshared)
int pthread_rwlockattr_setpshared ( pthread_rwlockattr_t * attr, int pshared)
int pthread_rwlockattr_getkind_np (const pthread_rwlockattr_t * attr, int * pref)
int pthread_rwlockattr_setkind_np ( pthread_rwlockattr_t * attr, int pref)
typedefpthread_spinlock_t
int pthread_spin_init ( pthread_spinlock_t * lock, int pshared)
int pthread_spin_destroy ( pthread_spinlock_t * lock)
int pthread_spin_lock ( pthread_spinlock_t * lock)
int pthread_spin_trylock ( pthread_spinlock_t * lock)
int pthread_spin_unlock ( pthread_spinlock_t * lock)
typedefpthread_barrier_t
int pthread_barrier_init ( pthread_barrier_t * barrier,
const pthread_barrierattr_t * attr, unsigned int count)
int pthread_barrier_destroy ( pthread_barrier_t * barrier)
int pthread_barrier_wait ( pthread_barrier_t * barrier)
typedefpthread_barrierattr_t
int pthread_barrierattr_init ( pthread_barrierattr_t * attr)
int pthread_barrierattr_destroy ( pthread_barrierattr_t * attr)
int pthread_barrierattr_getpshared (const pthread_barrierattr_t * attr, int * shared)
int pthread_barrierattr_setpshared ( pthread_barrierattr_t * attr, int shared)
typedefpthread_key_t
int pthread_key_create ( pthread_key_t * key, void (* destroy_function) (void *))
int pthread_key_delete ( pthread_key_t key)
int pthread_setspecific ( pthread_key_t key, const void * ptr)
void pthread_getspecific ( pthread_key_t key)
typedefpthread_once_t
int pthread_once ( pthread_once_t * control, void (* init_routine) (void))
int pthread_setcancelstate ( int state, int * oldstate)
int pthread_setcanceltype ( int type, int * oldtype)
int pthread_cancel ( pthread_t cancelthread)
void pthread_testcancel (void)
void pthread_cleanup_push ( void (* routine) (void *), void *arg)
void pthread_cleanup_pop ( int execute)
int pthread_getcpuclockid ( pthread_t thread_id, clockid_t * clock_id)
int pthread_atfork ( void (* prepare) (void),
void (* parent) (void),
void (* child) (void))
void pthread_kill_other_threads_np (void)
PTHREAD_MUTEX_STALLED
PTHREAD_MUTEX_ROBUST

Literatur

  1. Blaise Barney: POSIX Threads Programming, Lawrence Livermore National Laboratory