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