【Linux】应用篇八--线程的创建与回收



一、线程的概念与特点

1、线程的概念

  • 由于进程在切换时系统开销大
  • 很多操作系统引入了轻量级进程LWP(线程)
  • 同一进程中的线程共享相同地址空间
  • Linux不区分进程、线程
  • Linux的线程库是后来加入的,内核没有它的库,使用时得记得链接线程库

2、线程的特点

  • 通常线程指的是共享相同地址空间的多个任务
  • 使用多线程的好处,大大提高了任务切换的效率,避免了额外的TLB & cache的刷新

一个进程中的多个线程共享以下资源:

  • 可执行的指令
  • 静态数据
  • 进程中打开的文件描述符
  • 当前工作目录
  • 用户ID
  • 用户组ID

每个线程私有的资源包括:

  • 线程ID (TID)
  • PC(程序计数器)和相关寄存器
  • 堆栈
  • 错误号 (errno)
  • 优先级
  • 执行状态和属性

二、线程的创建

 #include  <pthread.h>
 int  pthread_create(pthread_t *thread, const
 pthread_attr_t *attr, void *(*routine)(void *), void *arg);

成功返回0失败时返回错误码

  • thread是线程对象
  • attr 线程属性,NULL代表默认属性
  • routine 线程执行的函数
  • arg 传递给routine的参数 ,参数是void * ,注意传递参数格式

编译错误分析:
1-参数不符

createP_t.c:14:36: warning: 
passing argument 3 of ‘pthread_create’ from 
incompatible pointer type [-Wincompatible-pointer-types]
 ret = pthread_create(&tid,NULL,testThread,NULL);
                                ^
In file included from createP_t.c:1:0:
/usr/include/pthread.h:233:12: note: 
expected ‘void * (*)(void *)’ but 
argument is of type ‘int * (*)(char *)’

意义:表示pthread_create参数3的定义和实际代码不符合,期望的是void * (*)(void *),实际的代码是int * (*)(char *)
解决方法:改为pthread_create(&tid,NULL,(void*)testThread,NULL);

2-链接错误

createP_t.c:(.text+0x4b):对‘pthread_create’未定义的引用
collect2: error: ld returned 1 exit status   --------这个链接错误,

表示pthread_create这个函数没有实现
解决方法:编译时候加 -lpthread

注意事项:

  • 主进程退出,它创建的线程也会退出
  • 线程创建需要时间,如果主进程马上退出,那线程不能得到执行

三、线程查看

1、获取线程的id

通过pthread_create函数的第一个参数;通过在线程里面调用pthread_self()函数,pthread_self()函数返回所在线程的id号。

#include <pthread.h>
pthread_t pthread_self(void);

2、线程结束

#include  <pthread.h>
 void  pthread_exit(void *retval);
  • 结束当前线程
  • retval可被其他线程通过pthread_join获取
  • 线程私有资源被释放
  • 尽量使用这个函数代替return

四、线程参数的传递

pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*routine)(void *), void *arg);
最后一个参数

编译错误:

createP_t.c:8:34: warning: dereferencing ‘void *’ pointer
 printf("input arg=%d\n",(int)*arg);
                              ^
createP_t.c:8:5: error: invalid use of void expression
 printf("input arg=%d\n",(int)*arg);

错误原因:是void *类型指针不能直接用*取值(*arg),因为编译不知道数据类型。
解决方法:转换为指定的指针类型后再用*取值 比如:*(int *)arg

  • 通过地址传递参数,注意类型的转换
  • 直接将值传递,这时候编译器会告警,需要程序员自己保证数据长度正确

线程查看命令ps -eLf

五、线程的回收

1、使用pthread_join 函数

#include  <pthread.h>
 int  pthread_join(pthread_t thread, void **retval);

对于一个默认属性的线程 A 来说,线程占用的资源并不会因为执行结束而得到释放

成功返回0失败时返回错误码

  • thread是要回收的线程对象
  • 调用线程阻塞直到thread结束
  • *retval 接收线程thread的返回值

2、使用线程的分离1

int pthread_detach(pthread_t thread)

成功返回0错误返回错误码
将该函数放在线程内,指定该状态,线程主动与主控线程断开关系。线程结束后(不会产生僵尸线程)


3、使用线程的分离2

pthread_attr_t attr;            /*通过线程属性来设置游离态(分离态)*/

创建线程时设置线程属性为分离

pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

到这里就结束啦!
在这里插入图片描述

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Linux 下的线程接口是 POSIX 线程(pthread)库,它是一套标准的 API,定义了线程的创建、同步、通信等操作。下面绍一些常用的函数和操作: ### 线创建 在 POSIX 线程库中,线程的创建使用 `pthread_create` 函数。它的原型如下: ```c int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); ``` 其中: - `thread`:指向线程标识符的指针。 - `attr`:指向线程属性的指针,如果为 `NULL`,表示使用默认属性。 - `start_routine`:线程的入口函数,它必须是一个指向函数的指针,且返回类型为 `void *`,接受一个 `void *` 类型的参数。 - `arg`:传递给线程入口函数的参数。 `pthread_create` 函数成功时返回 0,否则返回错误码。 ### 线程等待 线程等待使用 `pthread_join` 函数,它的原型如下: ```c int pthread_join(pthread_t thread, void **retval); ``` 其中: - `thread`:线程标识符。 - `retval`:指向线程返回值的指针,如果不需要返回值,可以传递 `NULL`。 `pthread_join` 函数会阻塞当前线程,直到指定的线程退出并返回。如果线程已经被分离,或者已经退出,`pthread_join` 函数会立即返回,并且不会修改 `retval` 指向的值。`pthread_join` 函数成功时返回 0,否则返回错误码。 ### 线程分离 线程分离使用 `pthread_detach` 函数,它的原型如下: ```c int pthread_detach(pthread_t thread); ``` 其中: - `thread`:线程标识符。 `pthread_detach` 函数将指定的线程标记为分离状态,它的资源会在线程退出时自动被回收,无法再次被等待。如果一个线程已经被分离,再次调用 `pthread_detach` 函数会返回错误码。`pthread_detach` 函数成功时返回 0,否则返回错误码。 ### 线程取消 线程取消使用 `pthread_cancel` 函数,它的原型如下: ```c int pthread_cancel(pthread_t thread); ``` 其中: - `thread`:线程标识符。 `pthread_cancel` 函数向指定的线程发送一个取消请求,如果线程允许取消,它将会退出。线程可以通过设置取消状态和取消类型来控制取消的行为。`pthread_cancel` 函数成功时返回 0,否则返回错误码。 ### 线程同步 线程同步使用互斥锁、条件变量等机制来实现。这些机制可以通过 pthread 库提供的函数来使用,如 `pthread_mutex_init`、`pthread_mutex_lock`、`pthread_cond_init`、`pthread_cond_wait` 等等。这些函数的使用方法比较复杂,需要根据具体的应用场景来选择和使用。 以上是一些常用的 POSIX 线程库的函数和操作,使用时需要根据具体的业务需求进行选择和使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

修成真

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值