pthread_t pthread_self (void); int pthread_equal (pthread_t __thread1, pthread_t __thread2);
int pthread_create (pthread_t *__restrict __newthread, const pthread_attr_t *__restrict __attr, void *(*__start_routine) (void *), void *__restrict __arg); void pthread_exit (void *__retval); // 等待線程結束 int pthread_join (pthread_t __th, void **__thread_return); // 分離線程 int pthread_detach (pthread_t __th); // 請求線程取消 int pthread_cancel (pthread_t __th);
// 保證 initfn 在多線程下只被調用一次
int pthread_once(pthread_once_t *initflag, void (*initfn) (void));
enum { PTHREAD_CREATE_JOINABLE, PTHREAD_CREATE_DETACHED }; int pthread_attr_init (pthread_attr_t *__attr); int pthread_attr_destroy (pthread_attr_t *__attr); // 分離狀態 int pthread_attr_getdetachstate (const pthread_attr_t *__attr, int *__detachstate); int pthread_attr_setdetachstate (pthread_attr_t *__attr, int __detachstate); // 自定義棧 int pthread_attr_getstack (const pthread_attr_t *__restrict __attr, void **__restrict __stackaddr, size_t *__restrict __stacksize); int pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr, size_t __stacksize); // 自定義棧大小 int pthread_attr_getstacksize (const pthread_attr_t *__restrict __attr, size_t *__restrict __stacksize); int pthread_attr_setstacksize (pthread_attr_t *__attr, size_t __stacksize); // 擴展棧大小(警惕區) int pthread_attr_getguardsize (const pthread_attr_t *__attr, size_t *__guardsize); int pthread_attr_setguardsize (pthread_attr_t *__attr, size_t __guardsize);
例子如 errno 變量,不一樣線程指向不一樣的地址空間。安全
// 建立一個線程存儲 int pthread_key_create(pthread_key_t *key, void (*destructor)(void*)); // 刪除線程存儲,不會調用 destructor int pthread_key_delete(pthread_key_t key); // 讀取 void *pthread_getspecific(pthread_key_t key); // 寫入 int pthread_setspecific(pthread_key_t key, const void *value);
模型:一個線程調用 sigwait() 專門處理信號,其它線程調用 pthread_sigmask() 阻塞信號多線程
int sigwait (const sigset_t *set, int *sig); int pthread_sigmask (int how /* = SIG_BLOCK or SIG_UNBLOCK or SIG_SETMASK */,const sigset_t *set,sigset_t *oset); int pthread_kill (pthread_t thread, int sig);
到達取消點(部分系統調用和庫函數)或 pthread_testcancel() 時,若是由取消請求,線程終止。函數
int pthread_setcancelstate (int __state /* = PTHREAD_CANCEL_ENABLE or PTHREAD_CANCEL_DISABLE */, int *__oldstate); int pthread_setcanceltype (int __type /* = PTHREAD_CANCEL_DEFERRED or PTHREAD_CANCEL_ASYNCHRONOUS */, int *__oldtype); void pthread_testcancel (void);
int pthread_mutex_init (pthread_mutex_t *__mutex, const pthread_mutexattr_t *__mutexattr); int pthread_mutex_destroy (pthread_mutex_t *__mutex); int pthread_mutex_lock (pthread_mutex_t *__mutex); int pthread_mutex_unlock (pthread_mutex_t *__mutex); int pthread_mutex_trylock (pthread_mutex_t *__mutex); int pthread_mutex_timedlock (pthread_mutex_t *__restrict __mutex, const struct timespec *__restrict __abstime); int pthread_mutexattr_init (pthread_mutexattr_t *__attr); int pthread_mutexattr_destroy (pthread_mutexattr_t *__attr); // 進程共享 int pthread_mutexattr_getpshared (const pthread_mutexattr_t * __restrict __attr, int *__restrict __pshared); int pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr, int __pshared);
int pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock, const pthread_rwlockattr_t *__restrict __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 *__restrict __rwlock, const struct timespec *__restrict __abstime); // 寫鎖 int pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock); int pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock); int pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock, const struct timespec *__restrict __abstime); int pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
條件變量 pthread_cond_wait() 作的事情:解鎖互斥量,等待條件發生,鎖住互斥量。臨界區代碼用。線程
int pthread_cond_init (pthread_cond_t *__restrict __cond, const pthread_condattr_t *__restrict __cond_attr); int pthread_cond_destroy (pthread_cond_t *__cond); // 通知一個阻塞的線程 int pthread_cond_signal (pthread_cond_t *__cond); // 通知全部阻塞的線程 int pthread_cond_broadcast (pthread_cond_t *__cond); int pthread_cond_wait (pthread_cond_t *__restrict __cond, pthread_mutex_t *__restrict __mutex); int pthread_cond_timedwait (pthread_cond_t *__restrict __cond, pthread_mutex_t *__restrict __mutex, const struct timespec *__restrict __abstime);
不阻塞。rest
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);
用於等待多個線程到達同一點。blog
int pthread_barrier_init (pthread_barrier_t *__restrict __barrier, const pthread_barrierattr_t *__restrict __attr, unsigned int __count); int pthread_barrier_destroy (pthread_barrier_t *__barrier); int pthread_barrier_wait (pthread_barrier_t *__barrier);