pthread_t pthread_self(void); //返回线程id
// 创建线程
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
//退出线程
void pthread_exit(void *retval);
// 回收线程 阻塞等待,不能与detach一起用
int pthread_join(pthread_t thread, void **retval);
// 杀死线程
int pthread_cancel(pthread_t thread);
// 线程分离,不需要自己回收线程了,不能与join一起用
int pthread_detach(pthread_t thread);
//判断两个线程id是否相等
int pthread_equal(pthread_t t1, pthread_t t2);
/* Initialize a mutex. 总是返回0 */
int pthread_mutex_init (pthread_mutex_t *__mutex,
const pthread_mutexattr_t *__mutexattr)
/* Destroy a mutex. */
int pthread_mutex_destroy (pthread_mutex_t *__mutex)
/* Try locking a mutex. 如果锁了立即返回*/
int pthread_mutex_trylock (pthread_mutex_t *__mutex)
/* Lock a mutex. */
int pthread_mutex_lock (pthread_mutex_t *__mutex)
/* Unlock a mutex. */
int pthread_mutex_unlock (pthread_mutex_t *__mutex)
/*
* 以上函数成功时返回0 ,否则返回错误号
*/
-
读写锁(读共享,写独占,写优先级高,读多写少时可用)
// 初始化 int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr); // 便捷初始化 pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; // 销毁读写锁,回收资源 int pthread_rwlock_destroy(pthread_rwlock_t *rwlock); // 加读锁 int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock); // 加写锁 int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock); // 开锁 int pthread_rwlock_unlock(pthread_rwlock_t *rwlock); /* * 以上函数皆是成功时返回0 ,否则返回错误号 */
-
信号量semaphore
/*信号量初始化 *__sem: 定义的信号量,传出 * __pshared: 0 代表线程信号量 * 非0 代表进程信号量 * __value: 定义信号量的个数 */ int sem_init (sem_t *__sem, int __pshared, unsigned int __value); // 释放信号sem_t对象 int sem_destroy (sem_t *__sem); // 申请信号量 申请成功 value-- int sem_wait (sem_t *__sem); // 释放信号量 value++ int sem_post (sem_t *__sem): // 得到当前的sem的value,并放入SVAL中 int sem_getvalue (sem_t *__restrict __sem, int *__restrict __sval); /* * 以上函数成功时返回0 ,否则返回-1, errno也会被设置 */ 以下函数待更新: sem_t *sem_open (const char *__name, int __oflag, ...); /* Close descriptor for named semaphore SEM. */ int sem_close (sem_t *__sem) __THROW; /* Remove named semaphore NAME. */ int sem_unlink (const char *__name) __THROW; /* Similar to `sem_wait' but wait only until ABSTIME. *This function is a cancellation point and therefore not marked *with __THROW. */ int sem_timedwait (sem_t *__restrict __sem, const struct timespec *__restrict __abstime); /* Test whether SEM is posted. */ int sem_trywait (sem_t *__sem) __THROWNL;
-
线程局部变量(待更新)
thread_local
-
查看当前pthread版本库 (NPTL)
getconf GNU_LIBPTHREAD_VERSION