APUE:線程,線程控制

線程標識

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);

  

可重入和線程安全

  • 線程安全:多個線程能夠同時的、安全的調用同一個函數
  • 重入:例如 main() 調用 malloc() 時產生信號,中斷原有流程,進入信號處理函數,信號處理函數中再次調用 malloc(),稱爲重入。從棧幀上說,malloc() 出現了屢次。此時調用 malloc() 顯然有問題,好比可能會破壞原有分配鏈表,可能會致使程序崩潰,因此 malloc() 是不可重入的。
  • 可重入函數確定是線程安全的,但線程安全不必定可重入,malloc() 就是一個線程安全不可重入的例子。

 

線程特定數據(線程私有數據)

例子如 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);

 

互斥量(mutex)

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);

 

讀寫鎖(rwlock)

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);

 

條件變量(cond)

條件變量 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);

 

自旋鎖(spin)

不阻塞。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);

 

屏障(barrier)

用於等待多個線程到達同一點。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);
相關文章
相關標籤/搜索