Linux多线程开发
一、线程
概念
线程(thread)是一种允许应用程序并发运行多个任务的机制。
线程之间共享全局数据区,以及堆区,创建代价小。
进程是CPU分配资源的最小单位,线程是操作系统调度执行的最小单位。
线程是轻量级进程(LWP: Light Weight Process),在Linux环境下,其本质仍是进程
查看指定的进程的LWP号:ps -Lf pid
区别
进程间的信息难以共享,需要通过进程间通信来进行信息交互
fork的代价较高,尽管已经使用了写时复制,读时共享技术;仍需要复制PCB信息,如内存页表,文件描述符表等。
线程只需要将数据复制到共享变量(全局或堆)中即可共享信息。
创建线程比创建进程快10倍甚至更多。无需写时复制,也无需复制页表。
NPTL,或称为 Native Thread Library,是Linux线程的一个新实现,满足了POSIX需求,提升了性能和稳定性。
查看当前pthread版本库:getconf GNU_LIBPTHREAD_VERSION
线程操作
默认main函数所在的线程为主线程,其余为子线程。
#includepthread_t pthread_self(void); 功能:获取当前的线程的线程ID
int pthread_equal(pthread_t t1, pthread_t t2); 功能:比较两个线程ID是否相等 不同的操作系统,pthread_t类型的实现不一样,有的是无符号的长整型,有的 是使用结构体去实现的。 #include
线程属性
线程属性类型 pthread_attr_t 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); - 设置线程分离的状态属性
二、线程同步
概念
线程通过全局变量来共享信息,十分便捷;但也会导致问题,多个线程同时修改同一变量。
临界区:访问共享资源的代码片段,且为原子操作。
线程同步:一次只能有一个线程操作内存。
互斥量
互斥量(mutex,mutual exclusion)可以保证对共享资源的原子访问(同时仅有一个线程访问资源)
使用时:先加锁,再访问共享资源,最后解锁
API介绍
互斥量的类型 pthread_mutex_t int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr); - 初始化互斥量 - 参数 : - mutex : 需要初始化的互斥量变量 - attr : 互斥量相关的属性,NULL - restrict : C语言的修饰符,被修饰的指针,不能由另外的一个指针进行操作。 pthread_mutex_t *restrict mutex = xxx; pthread_mutex_t * mutex1 = mutex;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); - 解锁 死锁 多个进程在执行过程中,因争夺共享资源而造成的一种互相等待的现象。 原因:忘记释放锁;重复加锁;多线程多锁,抢占资源
读写锁
有时,我们希望共享读,独占写;这就是读写锁的需求。
特点
- 如果有其它线程读数据,则允许其它线程执行读操作,但不允许写操作。
- 如果有其它线程写数据,则其它线程都不允许读、写操作。
- 写是独占的,写的优先级高。
API介绍
读写锁的类型 pthread_rwlock_t 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_wrlock(pthread_rwlock_t *rwlock); int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock); int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);条件变量
条件变量的类型 pthread_cond_t 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_t *restrict mutex); - 等待,调用了该函数,线程会阻塞。 int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime); - 等待多长时间,调用了这个函数,线程会阻塞,直到指定的时间结束。 int pthread_cond_signal(pthread_cond_t *cond); - 唤醒一个或者多个等待的线程 int pthread_cond_broadcast(pthread_cond_t *cond); - 唤醒所有的等待的线程信号量
信号量的类型 sem_t int sem_init(sem_t *sem, int pshared, unsigned int value); - 初始化信号量 - 参数: - sem : 信号量变量的地址 - pshared : 0 用在线程间 ,非0 用在进程间 - value : 信号量中的值int sem_destroy(sem_t *sem); - 释放资源
int sem_wait(sem_t *sem); - 对信号量加锁,调用一次对信号量的值-1,如果值为0,就阻塞
int sem_trywait(sem_t *sem);
int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout); int sem_post(sem_t *sem); - 对信号量解锁,调用一次对信号量的值+1
int sem_getvalue(sem_t *sem, int *sval);
sem_t psem; sem_t csem; init(psem, 0, 8); init(csem, 0, 0);
producer() { sem_wait(&psem); sem_post(&csem) }
customer() { sem_wait(&csem); sem_post(&psem) }