本篇文章是自己原来记录的笔记,关于 Linux 原生线程库 pthread 的一些用法。当初,记得比较零散,现在整理一下。
intro
pthread(也叫做 POSIX 线程)库是 Linux 中最常用的原生线程库,各大 Linux 发行版基本都默认安装了该库,并且 pthread 的 API 是基于 C 语言来设计的,所以在 Linux 下 C/C++ 的工程中使用起来十分方便,现在有很多项目也仍然在使用 pthread。
不过,C++ 从 C++11 开始有了自己的跨平台线程库 thread(之前好像用的是 boost?C++ 的线程库估计选择很多),若是支持 C++11 及以上的工程,还是推荐使用 C++ 自带的线程库;C 语言从 C11 开始也有了自己的跨平台线程库 threads.h,但据说好像各大编译器支持的不是很好?
好了,闲话少说,直入主题吧。
thread
首先从线程开始,pthread_t
是线程类型标识符,使用时需要在源代码文件中引入头文件pthread.h
,同时在编译时需要加上链接库-lpthread
。
比如,源代码文件:1
2
3
4
5
6
7
8
9
int main() {
// 创建线程标识符 t
pthread_t t;
return 0;
}
编译时:1
$ gcc test.c -o -pthread test
下面,我们再来看看相关的 API。
使用下列 API 需要引入头文件pthread.h
。
pthread_create:
功能:创建新线程。
函数原型:1
2int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*callback)(void *), void *arg);
thread
:用于接收新线程的 id,通过pthread_t
创建(注意这里是个指针)。attr
:线程属性,默认为NULL
。callback
:回调函数,由线程来执行。arg
:传给回调函数的参数,也可以为NULL
。
pthread_exit
功能:终止当前线程。
函数原型:1
void pthread_exit(void *retval);
retval
:线程退出时的返回值,可由pthread_join
捕获。
pthread_join
功能:阻塞等待线程结束并回收资源。
函数原型:1
int pthread_join(pthread_t thread, void **retval);
thread
:目标线程。retval
:线程退出时的返回值的指针。
pthread_detach
功能:分离线程(线程终止时自动回收资源)。
函数原型:1
int pthread_detach(pthread_t thread);
thread
:目标线程。
pthread_cancel
功能:取消指定线程。
函数原型:1
int pthread_cancel(pthread_t thread);
thread
:目标线程。
pthread_equal
功能:比较两个线程ID是否相同。
函数原型:1
int pthread_equal(pthread_t t1, pthread_t t2);
t1
:线程 1.t2
:线程 2。
mutex
接着介绍互斥锁,pthread_mutex_t
是互斥锁类型标识符,使用时需要在源代码文件中引入头文件pthread.h
,同时在编译时需要加上链接库-lpthread
,创建方法与pthread_t
类似,不再赘述。
使用下列 API 需要引入头文件pthread.h
。
pthread_mutex_init
功能:初始化互斥锁。
函数原型:1
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
mutex
:互斥锁变量的指针,用于接收新创建的互斥锁 id。attr
:互斥锁属性,默认为NULL
。
pthread_mutex_destroy
功能:销毁互斥锁。
函数原型:1
int pthread_mutex_destroy(pthread_mutex_t *mutex);
mutex
:目标互斥锁变量的指针。
pthread_mutex_lock
功能:加锁(阻塞直到获得锁)。
函数原型:1
int pthread_mutex_lock(pthread_mutex_t *mutex);
mutex
:目标互斥锁变量的指针。
pthread_mutex_unlock
功能:解锁
函数原型:1
int pthread_mutex_unlock(pthread_mutex_t *mutex);
mutex
:目标互斥锁变量的指针。
pthread_mutex_trylock
功能:尝试加锁(非阻塞,失败返回错误码)。
函数原型:1
int pthread_mutex_trylock(pthread_mutex_t *mutex);
mutex
:目标互斥锁变量的指针。
rwlock
然后是读写锁,pthread_rwlock_t
是读写锁类型标识符,使用时需要在源代码文件中引入头文件pthread.h
,同时在编译时需要加上链接库-lpthread
,创建方法与pthread_t
类似,不再赘述。
使用下列 API 需要引入头文件pthread.h
。
pthread_rwlock_init
功能:初始化读写锁。
函数原型:1
int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr);
rwlock
:读写锁变量的指针,用于接收新创建的读写锁 id。attr
:读写锁属性,默认为NULL
。
pthread_rwlock_destroy
功能:销毁读写锁。
函数原型:1
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
rwlock
:目标读写锁变量的指针。
pthread_rwlock_rdlock
功能:读模式加锁。
函数原型:1
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
rwlock
:目标读写锁变量的指针。
pthread_rwlock_wrlock
功能:写模式加锁。
函数原型:1
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
rwlock
:目标读写锁变量的指针。
pthread_rwlock_unlock
功能:解锁。
函数原型:1
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
rwlock
:目标读写锁变量的指针。
pthread_rwlock_tryrdlock
功能:尝试读模式加锁(非阻塞)。
函数原型:1
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
rwlock
:目标读写锁变量的指针。
pthread_rwlock_trywrlock
功能:尝试写模式加锁(非阻塞)。
函数原型:1
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
rwlock
:目标读写锁变量的指针。
condition variable
然后就是条件变量,pthread_cond_t
是条件变量类型标识符,使用时需要在源代码文件中引入头文件pthread.h
,同时在编译时需要加上链接库-lpthread
,创建方法与pthread_t
类似,不再赘述。
使用下列 API 需要引入头文件pthread.h
。
pthread_cond_init
功能:初始化条件变量。
函数原型:1
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
rwlock
:条件变量的指针,用于接收新创建的条件变量。attr
:条件变量属性,默认为NULL
。
pthread_cond_destroy
功能:销毁条件变量。
函数原型:1
int pthread_cond_destroy(pthread_cond_t *cond);
cond
:目标条件变量的指针。
pthread_cond_wait
功能:阻塞等待条件成立(需与互斥锁配合)。
函数原型:1
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
cond
:目标条件变量的指针。mutex
:目标互斥锁的指针。
pthread_cond_timedwait
功能:限时等待条件成立。
函数原型:1
2int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
const struct timespec *abstime);
cond
:目标条件变量的指针。mutex
:目标互斥锁的指针。abstime
:绝对时间戳,从 1970-1-1 开始计算。
pthread_cond_signal
功能:唤醒至少一个等待线程。
函数原型:1
int pthread_cond_signal(pthread_cond_t *cond);
cond
:目标条件变量的指针。
pthread_cond_broadcast
功能:唤醒所有等待线程。
函数原型:1
int pthread_cond_broadcast(pthread_cond_t *cond);
cond
:目标条件变量的指针。
semaphore
最后是信号量,sem_t
是信号量类型标识符,使用时需要在源代码文件中引入头文件semaphore.h
,一般配合线程使用,创建方法与pthread_t
类似,不再赘述。
使用下列 API 需要引入头文件semaphore.h
。
sem_init
功能:初始化信号量。
函数原型:1
int sem_init(sem_t *sem, int pshared, unsigned int value);
sem
:信号量的指针,用于接收新创建的信号量。pshared
:0 为进程内线程共享,非 0 为进程间共享。value
:信号量的初始值。
sem_destroy
功能:销毁信号量。
函数原型:1
int sem_destroy(sem_t *sem);
sem
:目标信号量的指针。
sem_wait
功能:资源减 1(资源为 0 时阻塞)。
函数原型:1
int sem_wait(sem_t *sem);
sem
:目标信号量的指针。
sem_trywait
功能:信号量资源数尝试减 1(资源为 0 时直接返回)。
函数原型:1
int sem_trywait(sem_t *sem);
sem
:目标信号量的指针。
sem_post
功能:信号量资源加 1(唤醒等待线程)。
函数原型:1
int sem_post(sem_t *sem);
sem
:目标信号量的指针。
sem_getvalue
功能:获取当前资源数量。
函数原型:1
int sem_getvalue(sem_t *sem, int *sval);
sem
:目标信号量的指针。sval
:信号量资源个数的指针。
summary
没什么营养的一篇流水文章,也没什么好说的,忘记了就回来看一看。