线程基本概念

线程

1. 概念
1.1 线程

线程是系统调度的基本单位

每一个进程都会有一个0-4G的内存空间,意味着5个进程就会有5个0-4G的空间,但是这样做,对于系统而言,会存在频繁的地址空间的切换,因此开销比较大,所以提出了轻量级进程–线程

但是对于操作系统而言,是不分进程和线程的,因为他们都是采用task_struct类型的变量来表示的

1.2 线程共享资源

可执行的指令

静态数据

进程中打开的文件描述符

当前工作目录

用户ID

用户组ID

1.3 线程私有资源

线程ID (TID)

PC(程序计数器)和相关寄存器

堆栈

错误号 (errno)

优先级

执行状态和属性

2. 相关API
2.1 线程创建
#include <pthread.h>

int pthread_creat(pthread_t *thread,const pthread_attr_t *attr,void*(*routine)(void*),void *arg )
	thread:线程对象
	attr:线程属性,NULL代表默认属性
	routine:线程执行的函数
	arg:传递给routine的参数,参数是void*,注意传递参数格式
	成功返回0,失败返回错误码
2.2 线程结束
#include <pthread.h>
void pthread_exit(void *retval)
	retval可悲其他线程通过pthread_join获取
	结束当前线程
	线程私有资源被释放
2.3 线程查看线程号
#include <pthread.h>
pthread_t pthread_self(void) //查看自己的线程号	
2.4 线程间参数传递
pthread_create(pthread_t *thread,const pthread_attr_t *attr,void *(*routine)(void *),void *arg)
	最后一个参数就是传递给线程的
2.5 线程回收

对于属性是joinable就可以使用 pthread_join() 来阻塞的等待一个线程的结束,并回收其资源,并且pthread_join() 还会得到线程退出后的返回值,来判断线程的退出状态 。

#include <pthread.h>

int pthread_join(pthread_t thread,void **retval)
	对于一个默认属性的线程A来说,线程占用的资源并不会因为执行结	  束而得到释放
	thread:要回收的线程对象
	*retval:接收线程thread的返回值
	未回收到资源,阻塞
2.6 线程分离

对于不需要返回值的线程就可以将其设置为detachd状态,也就是线程分离。

线程分离:线程注定与主控线程断开关系。线程结束后,系统来回收资源

第一种办法,使用pthread_detach函数设置线程分离

int pthread_detach(pthread_t thread);    
	成功:0;失败:错误号
	

第二种办法,在创建线程时设置为detached状态

设置线程属性为分离
pthread_attr_t attr;            
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
在创建线程时传入 attr的地址
2.7 线程取消
int pthread_cancel(pthread_t thread);   //杀死一个线程
有条件,要求线程中得有阻塞,否则设置一个取消点
void pthread_testcancel(void);			//设置取消点	

//设置能否取消
int pthread_setcancelstate(int state,int*oldstate);
	PTHREAD_CANCEL_ENABLE
	PTHREAD_CANCEL_DISABLE
//设置线程什么时候取消	
int pthread_setcanceltype(int type, int *oldtype);
	PTHREAD_CANCEL_DEFERRED 	//等到取消点才取消     
	PTHREAD_CANCEL_ASYNCHRONOUS //目标线程会立即取消    
2.8 线程清理

这两个宏得成对出现

void pthread_cleanup_push(void (*routine) (void *), void *arg)
    routine 函数被执行的条件:
    1.	被pthread_cancel取消掉。
    2.	执行pthread_exit 
    3.	非0参数执行pthread_cleanup_pop()

void pthread_cleanup_pop(int execute)

注意:

  1. 必须成对使用,即使pthread_cleanup_pop不会被执行到也必须写上,否则编译错误。
  2. pthread_cleanup_pop()被执行且参数为0,pthread_cleanup_push回调函数routine不会被执行.
  3. pthread_cleanup_push 和pthread_cleanup_pop可以写多对,routine执行顺序正好相反
  4. 线程内的return 可以结束线程,也可以给pthread_join返回值,但不能触发pthread_cleanup_push里面的回调函数,所以我们结束线程尽量使用pthread_exit退出线程。
3. 线程查看命令
ps -eLf | grep xxx
4. 线程gdb调试
显示线程
info thread 
切换线程
thread xxx

GDB为特定线程设置断点
break location thread id
GDB设置线程锁
set scheduler-locking on/off

5. 线程通信

临界资源

一次只允许一个任务(进程、线程)访问得共享资源

临界区

访问临界资源的代码

5.1 互斥锁
动态创建互斥锁
#include <pthread.h>

int pthread_mutex_init(pthread_mutex_t *mutex,const pthread_mutexattr_t * attr);
	mutex  指向要初始化的互斥锁对象
 	attr  互斥锁属性,NULL表示缺省属性
	成功时返回0,失败时返回错误码
静态创建互斥锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
锁的销毁
int pthread_mutex_destroy(pthread_mutex_t *mutex)
在Linux中,互斥锁并不占用任何资源,因此LinuxThreads中的 pthread_mutex_destroy()除了检查锁状态以外(锁定状态则返回EBUSY)没有其他动作。
申请锁
#include <pthread.h>
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex)
 	成功时返回0,失败时返回错误码
 	mutex  指向要初始化的互斥锁对象
 	
pthread_mutex_lock 如果无法获得锁,任务阻塞
pthread_mutex_trylock 如果无法获得锁,返回EBUSY而不是挂起等待

释放锁
#include <pthread.h>
int pthread_mutex_unlock(pthread_mutex_t *mutex);
	成功时返回0,失败时返回错误码
 	mutex  指向要初始化的互斥锁对象
 	执行完临界区要及时释放锁

5.2 读写锁
必要性

提高线程执行效率

特性

写者:写者使用写锁,如果当前没有读者,也没有其他写者,写者立即获得写锁;否则写者将等待,直到没有读者和写者。

读者:读者使用读锁,如果当前没有写者,读者立即获得读锁;否则读者等待,直到没有写者。

注意

同一时刻只有一个线程可以获得写锁,同一时刻可以有多个线程获得读锁。

读写锁出于写锁状态时,所有试图对读写锁加锁的线程,不管是读者试图加读锁,还是写者试图加写锁,都会被阻塞。

读写锁处于读锁状态时,有写者试图加写锁时,之后的其他线程的读锁请求会被阻塞,以避免写者长时间的不写锁

初始化一个读写锁
pthread_rwlock_init
读锁定读写锁
pthread_rwlock_rdlock
非阻塞读锁定
pthread_rwlock_tryrdlock
写锁定读写锁
pthread_rwlock_wrlock
非阻塞写锁定
pthread_rwlock_trywrlock
解锁读写锁
pthread_rwlock_unlock
释放读写锁
pthread_rwlock_destroy
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值