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)

不阻塞。

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)

用于等待多个线程到达同一点。

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

  

posted @ 2017-05-26 13:40  mfmans  阅读(93)  评论(0编辑  收藏  举报