相关函数列表
//对两个线程ID进行比较
//若相等返回非0数值,否则返回0
#include <pthread.h>
int pthread_equal(pthread_t tid1, pthread_t tid2);
//获得线程自身的ID
#include <pthread.h>
pthread_t pthread_self(void);
//创建新线程
//pthread_attr_t用来定制各种不同的线程属性,新线程函数从start_rtn函数的地址开始运行,如果
//要向start_rtn传递参数,需要将这些参数放入结构体中,然后将此将结构体地址作为arg参数传入
#include <pthread.h>
int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr,
void *(*strt_rtn)(void *), void *restrict arg);
//单个线程可以通过3种方式退出
//1.线程可以简单地从启动例程种返回,返回值是线程的退出码
//2.线程可以被同一进程中的其他线程取消
//3.线程调用pthread_exit
#include <pthread.h>
void pthread_exit(void *rval_ptr);
//进程中的其他线程可以通过调用下列函数来访问这个 rval_ptr指针
//如果对线程的返回值不感兴趣,可以把rval_ptr设置为NULL,这样等于等待线程终止,但是不获取
//线程终止状态
#include <pthread.h>
int pthread_join(pthread_t thread, void **rval_ptr);
//线程取消
#include <pthread.h>
int pthread_cancel(pthread_t tid);
//可以安排一些清理函数,类似进程的atexit函数,这样的函数被称为线程清理处理程序(thread
//cleanup handler),一个线程可以建立多个清理处理程序,其执行顺序和注册顺序相反
#include <pthread.h>
void pthread_cleanup_push(void (*rtn)(void *), void *arg);
void pthread_cleanup_pop(int execute);
//分离线程
#include <pthread.h>
int pthread_detach(pthread_t tid);
//互斥量
//如果不希望被阻塞使用trylock函数,不出现阻塞直接返回0,否则就会失败不能锁住返回EBUSY
//timelock函数指定一个绝对时间(在X到达之前可以阻塞,而不是等待Y秒)
#include <pthread.h>
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
int pthread_mutex_destroy(pthread_mutex_t *mutex);
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
int pthread_mutex_timedlock(pthread_mutex_t *restrict mutex, const struct timespec *restrict tsptr);
//读写锁
#include <pthread.h>
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock);
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);
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_timerdlock(pthread_rwlock_t *restrict rwlock, const struct
timespec *restrict tsptr);
int pthread_rwlock_timewrlock(pthread_rwlock_t *restrict rwlock, const struct
timespec *restrict tsptr);
//条件变量
#include <pthread.h>
int pthread_cond_init(pthread_cond_t *restrict cond, const pthread-condattr_t *restrict attr);
int pthread_cond_destroy(pthread_cond_t *cond);
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex *restrict mutex);
int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex,
const struct timespec *restrict tsptr);
int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_broadcast(pthread_cond_t *cond);
//自旋锁
#include <pthread.h>
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);
//屏障
#include <pthread.h>
int pthread_barrier_init(pthread_barrier_t *restrict barrier, const pthread_barrierattr_t
*restrict attr, unsigned int count);
int pthread_barrier_destroy(pthred_barrier_t *barrier);
int pthread_barrier_wait(pthread_barrier_t *barrier);
注意事项
不要用直接操作pthread_t结构体
这样会导致代码不可移植,初始化结构体后,用相关函数操作
同样,pthread.h中的其他结构体也不要直接操作,初始化好之后用相关函数操作
因为pthread不是linux的标准库,所以GCC编译时,需要加上 -lpthread参数
进程和线程原语的比较
| 进程原语 | 线程原语 | 描述 |
| fork | pthread_create | 创建新的控制流 |
| exit | pthread_exit | 从现有的控制流中退出 |
| waitpid | pthread_join | 从控制流中得到退出状态 |
| atexit | pthread_cancel_push | 注册在退出控制流时调用的函数 |
| getpid | pthread_self | 获取控制流的ID |
| abort | pthread_cancel | 请求控制流的非正常退出 |
参考
本文详细介绍了线程原语及互斥量的基本概念、使用方法和注意事项,包括线程创建、退出、取消、返回值获取、互斥量初始化、销毁、加锁、尝试加锁、解锁等功能,旨在帮助开发者更高效地利用线程原语实现并发编程。
845

被折叠的 条评论
为什么被折叠?



