目录
一、线程
1、线程的概念
2、进程和线程的区别
3、线程的函数
1)安装线程的man手册
2)pthread_create
1. 分支线程结束,主线程不会结束
2. 主线程结束,分支线程也会结束
3. 分支线程和主线程都可以使用全剧变量
4. 分支线程使用主线程的局部变量
5. 主线程访问分支线程的局部变量
6. 主线程访问分支线程的变量的地址(使用分支线程的局部变量)
3)pthread_self(获取线程ID)
4)pthread_exit(结束线程)
5)pthread_ join(回收指定函数的资源)
6)pthread_detach(分离线程)
7)pthread_cancel(结束子线程)
8)pthread_setcancelstate(选择是否同意主进程的请求)
9)pthread_setcanceltype(选择取消的状态)
一、线程
1、线程的概念
线程:是进程并发执行多个任务(线程)的机制,同进程下的多个线程共用同一进程资源。
进程:是由一个或多个线程组成的
串行:多个任务在单核CPU上按顺序执行
并发:多个任务在单核CPU上,以时间片轮机制执行
并行:多核CPU同时执行多个任务
2、进程和线程的区别
1. 进程之间用户空间独立,内核资源共享,,线程共享进程的资源
2. 进程是资源分配日的最小单位,线程是任务执行的最小单位
3. 多进程之间需要引入进程间通讯机制(IPC),多线程之间需要引入同步互斥
4. 进程的资源比线程多
5. 线程的效率比进程高
6. 进程的稳定性比线程强
3、线程的函数
1)安装线程的man手册
安装线程:sudo apt-get install manpages-posix*
注意如果出现锁的错误,解决方式:删除以下两个文件
rm /var/lib/dpkg/lock lock-frontend
2)pthread_create
格式:
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);Compile and link with -pthread.
功能:在进程内创建一个新的线程
参数:pthread_t *thread:该指针指向的内存中存储线程id const pthread_attr_t *attr:线程的属性(线程的描述信息),写NULL,表示默认属性(结合态)可以通过pthread_attr_init 修改属性,分离态void *(*start_routine) (void *): 创建的线程函数名,该函数必须是函数指针arg:表示线程体的参数,可以写NULL返回值:成功返回0失败方error number,此时线程id未被定义

1. 分支线程结束,主线程不会结束
2. 主线程结束,分支线程也会结束
3. 分支线程和主线程都可以使用全剧变量
4. 分支线程使用主线程的局部变量

5. 主线程访问分支线程的局部变量

6. 主线程访问分支线程的变量的地址(使用分支线程的局部变量)

3)pthread_self(获取线程ID)
格式:#include <pthread.h>pthread_t pthread_self(void);Compile and link with -pthread. ---> 编译的时候需要加 -pthread
功能:获取调用线程的线程id
返回值:返回调用者的线程id
4)pthread_exit(结束线程)
格式:#include <pthread.h>void pthread_exit(void *retval);Compile and link with -pthread. ---> 编译的时候需要加 -pthread
功能:终止调用线程的,一般用于分支线程
参数:void *retval: 线程的返回值,可以写NULL
5)pthread_ join(回收指定函数的资源)
格式:#include <pthread.h>int pthread_join(pthread_t thread, void **retval);Compile and link with -pthread.
功能:阻塞函数,等待接收指定线程的资源,回收指定线程的资源
参数:pthread_t thread:阻塞等待接收的指定线程idvoid **retval:接收pthread_exit的退出状态值需要指向一级指针的地址,不接收可以写NULL
返回值:
成功返回0,失败返回error number
6)pthread_detach(分离线程)
格式:#include <pthread.h>int pthread_detach(pthread_t thread);Compile and link with -pthread.
功能:分离线程的,结束线程的资源有系统自动回收,不会阻塞一旦使用pthread_detach函数,则pthread_join则无效,不会阻塞回收资源
参数:pthread_t thread:分离的线程id
返回值:
成功返回0,失败返回error number
7)pthread_cancel(结束子线程)
格式:#include <pthread.h>int pthread_cancel(pthread_t thread); Compile and link with -pthread.
功能:对指定子线程发送一个取消(结束)请求(信号),子进程可以选择取消的状态,以及是否结束线程
参数:pthread_t thread:指定的子线程id
返回值:
成功返回0,失败返回error number
8)pthread_setcancelstate(选择是否同意主进程的请求)
格式:#include <pthread.h>int pthread_setcancelstate(int state, int *oldstate);
功能:设置取消的状态
参数:int state:PTHREAD_CANCEL_ENABLE,可以取消PTHREAD_CANCEL_DISABLE,不可以取消int *oldstate:获取上一次的取消状态值的,不想要获取写NULL
返回值:成功返回0,失败返回非0的error number
9)pthread_setcanceltype(选择取消的状态)
格式:int pthread_setcanceltype(int type, int *oldtype);Compile and link with -pthread.
功能:设置取消类型
参数:
int type:PTHREAD_CANCEL_DEFERRED:延时取消(遇到取消点:sleep,printf)PTHREAD_CANCEL_ASYNCHRONOUS:立刻取消
int *oldtype:获取上一次的取消类型值的,不想要获取写NULL
返回值:成功返回0,失败返回非0的error number
作业:
1. 创建一个分支线程,在主线程中拷贝文件的前一部分,分支线程拷贝文件的后一部分

2. 解读代码

info 1 from child procsee_1
info 1 from child procsee_1info 1 from child procsee
info 1 from child procsee_2
3. 解读代码(输出8次)
