linux之多线程

本文深入探讨了Linux中线程与进程的区别,指出在内核层面二者并无严格区分,线程主要是用户层面的概念。详细阐述了线程间的资源共享与独立资源,并列举了如pthread_create、pthread_join等线程管理函数。此外,还介绍了线程同步的各种机制,包括互斥锁、读写锁、条件变量和信号量,以及它们在多线程编程中的作用。
摘要由CSDN通过智能技术生成

1. linux中的线程

  • 在linux中,无论是创建进程的fork,还是创建线程的pthread_create,底层实现都是调用同一个内核函数clone
  • 如果复制对方的地址空间,那么就创建出一个进程,如果共享对方的地址空间,那么就产生一个线程。
  • 因此:linux内核是不区分进程和线程的。只是在用户层面上进行区分。
  • 线程所有的操作函数是库函数(第三方库),而非系统调用。

1.1 线程之间共享的资源

  • 文件描述符表
  • 每种信号的处理方式
  • 当前的工作目录
  • 用户ID和组ID
  • 内存地址空间

1.2 线程之间不共享的资源

  • 线程id
  • 处理器现场和内核栈
    • 每个线程需要有自己的内核栈空间,内核栈主要在线程切换的时候保存寄存器的值
  • 用户栈
    • 用户栈里面的存储栈帧,一个栈帧代表一个函数调用
  • errno变量
  • 信号屏蔽字
  • 调度优先级

2. 线程相关的函数

2.1 pthread_self函数:获取线程id

pthread_t pthread_self(void);
  • 返回值:线程id

2.2 pthread_create函数:创建线程

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);
  • 返回值:成功返回0,失败返回错误号
  • thread:传出参数,保存系统分配好的线程id
  • attr:线程默认属性
  • start_routine:函数指针,指向线程主函数。该线程运行结束,则线程结束
  • arg:线程主函数执行期间所用到的参数

2.3 pthread_exit函数:将当前线程退出

  • 该方法有线程自己调用
void pthread_exit(void *retval);
  • retval:泛型指针,可以传任意数据类型。线程退出返回的信息。

2.4 pthread_join函数:阻塞等待其他线程退出

  • 调用该函数会对线程的资源进行回收
int pthread_join(pthread_t thread, void **retval);
  • 返回值:成功返回0,失败返回错误号
  • thread:要等待退出的线程id
  • retval:传出参数,存储线程结束状态

2.5 pthread_detach函数:实现线程分离

  • 调用该线程后,线程结束后,其退出状态不由其他线程获取,而直接自己自动释放。从而不会产生僵尸线程。
int pthread_detach(pthread_t thread);
  • 返回值:成功返回0,失败返回错误号

2.6 pthread_cancel函数:杀死线程

int pthread_cancel(pthread_t thread);
  • 返回值:成功返回0,失败返回错误号

2.7 pthread_equal函数:比较两个线程id是否相等

int pthread_equal(pthread_t t1, pthread_t t2);
  • 在linux系统中没啥用,因为在pthread_t类型在linux下就是无符号整型

2.8 线程属性pthread_attr

typedef struct
{
    int 					etachstate; 	//线程的分离状态
    int 					schedpolicy; 	//线程调度策略
    struct sched_param	schedparam; 	//线程的调度参数
    int 					inheritsched; 	//线程的继承性
    int 					scope; 		//线程的作用域
    size_t 				guardsize; 	//线程栈末尾的警戒缓冲区大小
    int					stackaddr_set; //线程的栈设置
    void* 				stackaddr; 	//线程栈的位置
    size_t 				stacksize; 	//线程栈的大小
} pthread_attr_t;
  • 线程属性不能直接设置,需要用相关的函数来操作

2.9 pthread_attr_init函数:初始化线程属性

int pthread_attr_init(pthread_attr_t *attr);
  • 返回值:成功返回0,失败返回错误号
  • attr:传出参数,初始化好的线程属性

2.10 pthread_attr_destroy函数:销毁线程属性所占用的资源

int pthread_attr_destroy(pthread_attr_t *attr);
  • 返回值:成功返回0,失败返回错误号
  • attr:要销毁的线程属性

2.11 pthread_attr_setdetachstate函数:设置线程分离属性

int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate); 
  • 返回值:是否成功,失败返回错误号
  • detachstate
    • PTHREAD_CREATE_DETACHED(分离线程)
    • PTHREAD _CREATE_JOINABLE(非分离线程)

2.12 setstack函数:设置栈空间属性

  • 当进程栈地址空间不够用时,指定新建线程使用由malloc分配的空间作为自己的栈空间
  • 即在堆上分配空间(malloc)作为线程的栈空间
int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize); 
  • 返回值:是否成功,失败返回错误号
  • stackaddr:传入参数,堆空间中的一个地址,用于作为用户线程栈空间首地址。
  • stacksize:栈空间大小

2.13 getstack函数:获取栈空间属性

int pthread_attr_getstack(pthread_attr_t *attr, void **stackaddr, size_t *stacksize);
  • 返回值:是否成功,失败返回错误号
  • stackaddr:传出参数,栈空间的首地址
  • stacksize:传出参数,栈空间大小

2.14 设置线程的栈大小

int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);

int pthread_attr_getstacksize(pthread_attr_t *attr, size_t *stacksize);
  • 栈空间由内核指定,我们只需要指定大小即可

3. 线程同步之互斥锁相关函数

3.1 pthread_mutex_init函数:初始化一个互斥锁

int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
  • 返回值:是否成功
  • mutex:传出参数,互斥锁
  • attr:传入参数,互斥量属性
// 初始化互斥锁属性
int pthread_mutexattr_init(pthread_mutexattr_t *attr);

// 给互斥锁属性设置是否进程间共享
int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);

// 销毁互斥锁属性
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
  • pshared取值
    •  PTHREAD_PROCESS_PRIVATE(线程锁,默认)
    •  PTHREAD_PROCESS_SHARED(进程锁)

3.2 pthread_mutex_destroy函数:销毁一个互斥锁

int pthread_mutex_destroy(pthread_mutex_t *mutex);

3.3 pthread_mutex_lock函数:加锁

int pthread_mutex_lock(pthread_mutex_t *mutex);

3.4 pthread_mutex_unlock函数:解锁

int pthread_mutex_unlock(pthread_mutex_t *mutex);

3.5 pthread_mutex_trylock函数:尝试加锁(非阻塞)

int pthread_mutex_trylock(pthread_mutex_t *mutex);
  • 非阻塞函数,会立即返回结果
  • 如果加锁失败直接返回错误号:EBUSY

4. 线程同步之读写锁相关函数

4.1 pthread_rwlock_init函数:初始化读写锁

int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, 
                        const pthread_rwlockattr_t *restrict attr);
  • rwlock:传出参数,读写锁
  • attr:读写锁属性,通常传NULL

4.2 pthread_rwlock_destroy函数:销毁读写锁

int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);

4.3 pthread_rwlock_rdlock函数:加读锁

int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);

4.4 pthread_rwlock_wrlock函数:加写锁

int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);

4.5 pthread_rwlock_unlock函数:释放锁

int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);

4.6 非阻塞方式获取读锁和写锁

int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);

int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);

5. 线程同步之条件变量

  • 条件变量通常需要与互斥锁相互配合使用

5.1 pthread_cond_init函数:初始化一个条件变量

int pthread_cond_init(pthread_cond_t *restrict cond, 
                        const pthread_condattr_t *restrict attr);
  • 返回值:是否成功
  • cond:传出参数,条件变量
  • attr:条件变量属性,通常设置为NULL

5.2 pthread_cond_destroy函数:销毁一个条件变量

int pthread_cond_destroy(pthread_cond_t *cond);

5.3 pthread_cond_wait函数

  • 释放掉互斥锁,同时将线程自己挂在条件变量上阻塞。等待被唤醒。
 int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
  • cond:条件变量,当前线程要挂在哪个条件变量上阻塞
  • mutex:互斥锁,当前线程要释放的互斥锁

5.4 pthread_cond_timedwait函数

  • 释放掉互斥锁,同时将线程自身挂在条件变量上进行阻塞。到时间会自动解除阻塞,然后去争抢互斥锁
int pthread_cond_timedwait(pthread_cond_t *cond, 
                           pthread_mutex_t *mutex, 
                           const struct timespec *abstime);
  • cond:条件变量,当前线程要挂在哪个条件变量上进行阻塞
  • mutex:互斥锁,当前线程要释放哪个互斥锁
  • abstime:结构体,表示绝对时间
struct timespec {
			time_t tv_sec;		/* seconds */ 秒
			long   tv_nsec;	/* nanosecondes*/ 纳秒
		}

5.5 pthread_cond_signal函数:唤醒条件变量上的任意一个阻塞的线程

int pthread_cond_signal(pthread_cond_t *cond);

5.6 pthread_cond_broadcast函数:唤醒条件变量上的所有阻塞线程

int pthread_cond_broadcast(pthread_cond_t *cond);

6. 线程同步之信号量相关操作

6.1 sem_init函数:初始化一个信号量

int sem_init(sem_t *sem, int pshared, unsigned int value);
  • sem:传出参数,信号量
  • pshared:取0用于线程间,取非0用于进程间
  • value:信号量初值

6.2 sem_destroy函数:销毁一个信号量

int sem_destroy(sem_t *sem);

6.3 sem_wait函数:给信号量加锁

int sem_wait(sem_t *sem);

6.4 sem_post函数:给信号量解锁

int sem_post(sem_t *sem);	

6.5 sem_trywait函数:非阻塞尝试给信号量加锁

int sem_trywait(sem_t *sem);

6.6 sem_timedwait函数:

int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);
  • 也会阻塞等待加锁。
  • 但是如果在定时之内无法获取到锁,就返回错误号。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值