1. 线程
1.1概念
- 线程是一个轻量级的进程,为了提高系统的性能引入线程。
- 线程和进程都参与统一的调度。
- 在同一个进程中可以创建的多个线程, 共享进程资源。
- (Linux里同样用task_struct来描述一个线程)
1.2进程和线程的区别
相同点:都为操作系统提供了并发执行能力
不同点:
- 调度和资源:线程是系统调度的最小单位,进程是资源分配的最小单位
- 地址空间方面:同一个进程创建的多个线程共享该进程的资源;进程的地址空间相互独立
- 通信方面:线程通信相对简单,只需要通过全局变量可以实现,但是需要考虑临界资源访问的问题;进程通信比较复杂,需要借助进程间的通信机制(借助3g-4g内核空间)
- 安全性方面:线程安全性差一些,当进程结束时会导致所有线程退出;进程相对安全
什么时候用进程,什么时候用线程?
对资源的管理和保护要求高,不限制开销和效率时,使用多进程。
要求效率高、速度快的高并发环境时,需要频繁创建、销毁或切换时,资源的保护管理要求不是很高时,使用多线程。
1.3线程资源
- 共享的资源:可执行的指令、静态数据、进程中打开的文件描述符、信号处理函数、当前工作目录、用户ID、用户组ID
- 私有的资源:线程ID (TID)、PC(程序计数器)和相关寄存器、堆栈(局部变量, 返回地址)、错误号 (errno)、信号掩码和优先级、执行状态和属性
2.函数接口
2.1创建线程
int pthread_create( pthread_t *thread, const pthread_attr_t *attr,
void *( *start_routine)( void*), void *arg);
功能:创建线程
参数: thread:线程标识
attr:线程属性, NULL:代表设置默认属性
start_routine:函数名:代表线程函数(自己写的)
arg:用来给前面函数传参
返回值:成功:0
失败:错误码
编译的时候需要加 -pthread 链接动态库
#include <stdio.h>
#include <pthread.h>
void *handler_thread(void *arg)
{
printf(" in handler_thread \n");
while (1); // 从线程不结束
return NULL;
}
int main(int argc, char const *argv[])
{
pthread_t tid;
pthread_create(&tid, NULL, handler_thread, NULL);
if (pthread_create(&tid, NULL, handler_thread, NULL) != 0)
{
perror(" creat thread error");
return -1;
}
printf(" in main \n");
while (1); // 让进程不停止,避免线程结束导致线程推出
return 0;
}
2.2退出线程
void pthread_exit( void *value_ptr)
功能:用于退出线程的执行
参数:value_ptr:线程退出时返回的值
#include <stdio.h>
#include <pthread.h>
void *handler_thread(void *arg)
{
printf(" in handler_thread \n");
pthread_exit(NULL); // 让从线程退出
while (1); // 从线程不结束
return NULL;
}
int main(int argc, char const *argv[])
{
pthread_t tid;
pthread_create(&tid, NULL, handler_thread, NULL);
if (pthread_create(&tid, NULL, handler_thread, NULL) != 0)
{
perror(" creat thread error");
return -1;
}
printf(" in main \n");
while (1); // 让进程不停止,避免线程结束导致线程推出
return 0;
}
2.3回收线程资源
int pthread_join( pthread_t thread, void **value_ptr)
功能:用于等待一个指定的线程结束,阻塞函数
参数:thread:创建的线程对象,线程ID
value_ptr:指针 *value_ptr 指向线程返回的参数, 一般为NULL
返回值:成功 : 0
失败:errno
int pthread_detach( pthread_t thread);
功能:让线程结束时自动回收线程资源,让线程和主线程分离,非阻塞函数
参数:thread:线程ID
非阻塞式的,例如主线程分离(detach)了线程T2,那么主线程不会阻塞在pthread_detach(),pthread_detach()会直接返回,线程T2终止后会被操作系统自动回收资源
pthread_join为阻塞型的,如果从线程还没有结束,那么主线程会阻塞,会等待从线程结束之后才接触阻塞,回收从线程的资源。
pthread_detach为非阻塞型的,如果从线程没有结束,主线程不会阻塞,会直接返回,从线程终止后系统会自动回收资源。