目录
🌈前言
这篇文章给大家带来线程控制的学习!!!
🌸1、Linux线程控制
-
POSIX线程库(第三方库)
-
- Linux没有真正的线程,只是PCB模拟,所以不会提供线程接口,只有轻量级进程
-
- 这是Linux自带的原生库,定义了创建和操纵线程的一套API(函数接口),绝大多数函数的名字都是以“pthread_”为前缀
-
- 要使用这些函数库,要通过引入头文<pthread.h>
-
- 链接这些线程函数库时要使用编译器命令的“-lpthread”选项,因为它是第三方库
-
- 任何语言在Linux想要实现一套自己的线程库,都是封装POSIX线程库来实现的
🍡1.1、创建线程(pthread_create)
#include <pthread.h>
typedef unsigned long int pthread_t;
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
-
函数解析
-
- 作用:pthread_create()函数在调用过程中启动一个新线程
-
- thread:该参数返回一个线程ID,输出型参数,由0S填充
-
- attr:该参数是用于设置线程的属性,如果为空,则设置线程默认的属性
-
- start_routine:该参数是一个回调函数,线程启动后,会执行该函数的代码,返回值void*可以指定线程退出状态值
-
- arg:该参数是用来命名线程的,它会被传给线程启动函数的参数(start_routine)
-
- 返回值:启动成功返回0,启动失败返回错误码(线程有自己的错误码)
创建一个线程,并且让它跑起来
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <pthread.h>
using namespace std;
// |---------------------------------------------------------------------|
// | #include <pthread.h> |
// | int pthread_create(pthread_t *thread, const pthread_attr_t *attr, |
// | void *(*start_routine) (void *), void *arg); |
// |---------------------------------------------------------------------|
void *CallPthread1(void *arg)
{
printf("I am %s\n", (const char *)arg);
// 指定线程退出状态值
return (void *)0;
}
void *CallPthread2(void *arg)
{
printf("I am %s\n", (const char *)arg);
// 指定线程退出状态值
return (void *)0;
}
int main()
{
// 1、定义线程id,该id由OS填充
pthread_t tid1;
pthread_t tid2;
// 创建线程,并且填充t1的id,arg参数给线程命名,并且让线程执行CallPthread代码
if (pthread_create(&tid1, nullptr, CallPthread1, (void *)"thread t1") != 0)
{
exit(EXIT_FAILURE);
}
if (pthread_create(&tid2, nullptr, CallPthread2, (void *)"thread t2") != 0)
{
exit(EXIT_FAILURE);
}
return 0;
}
运行结果:
[lyh@192 Make_thread(1)]$ ./Thread_Creation
I am thread t1
I am thread t2
-
错误检查
-
- 传统的一些库函数是:成功返回0,失败返回-1,并且对全局变量errno赋值以指示错误
-
- 线程函数出错时,不会设置全局变量errno(但是大部分其他库函数会设置),而是通过线程所执行的函数的返回值将错误码返回
-
- 线程同样也提供了线程内的errno变量,每个线程都有属于自己的局部errno,以避免一个线程干扰另一个线程
-
- 对于线程函数的错误,建议通过返回值来判定,因为读取返回值要比读取线程内的errno变量的开销更小
🍢1.2、通过指令查看线程PID和LWP
-
前言
-
- 线程是进程的执行程序(执行流),它们的PID是一样的
-
- 但是,进程和轻量级进程(线程)ID(LWP)是不一样的
-
- 轻量级进程(线程)是CPU调度的基本单位,需要一个唯一的数值(LWP)来标识它
-
- 我们可以通过ps -aL查看全部正在运行的主线程(main函数)和新线程(pthread_create)的信息
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <pthread.h>
using namespace std;
// |---------------------------------------------------------------------|
// | #include <pthread.h> |
// | int pthread_create(pthread_t *thread, const pthread_attr_t *attr, |
// | void *(*start_routine) (void *), void *arg); |
// |---------------------------------------------------------------------|
void *CallPthread1(void *arg)
{
printf("I am %s, thread PID: %d\n", (const char *)arg, getpid());
sleep(3);
// 指定线程退出状态值
return (void *)0;
}
void *CallPthread2(void *arg)
{
printf("I am %s, thread PID: %d\n", (const char *)arg, getpid());
sleep(3);
// 指定线程退出状态值
return (void *)0;
}
int main()
{
// 1、定义线程id,该id由OS填充
pthread_t tid1;
pthread_t tid2;
// 创建线程,并且填充t1的id,arg参数给线程命名,并且让线程执行CallPthread代码
if (pthread_create(&tid1, nullptr, CallPthread1, (void *)"thread t1") != 0)
{
exit(EXIT_FAILURE);
}
if (pthread_create(&tid2, nullptr, CallPthread2, (void *)"thread t2") != 0)
{
exit(EXIT_FAILURE);
}
while (true)
{
cout << "我是主线程, pid: : " << getpid() << endl;
sleep(1);
}
return 0;
}
🍧1.3、获取线程ID(pthread_self)
🍸1.3.1、理解pthread_t类型
-
前言
-
- 线程是一个独立的执行流,不会受到其他线程的影响(除异常外)
-
- 线程一定会在运行过程中,产生临时数据(函数调用,定义局部变量等)
-
- 线程一定有自己的独立栈结构
创建线程链接库的过程
- 前面说了,Linux没有提供线程接口,只有第三方库,我们链接的动态库是libpthread.so.0
[lyh@192 Linux_Study]$ ls -al /usr/lib64/libpthread.so.0
lrwxrwxrwx. 1 root root 18 Dec 12 02:23 /usr/lib64/libpthread.so.0 -> libpthread-2.17.so
理解pthread_t类型
-
pthread_t原理
-
- pthread_t类型其实是一个虚拟内存的地址
-
- 当主线程创建线程时,会自动填充线程id,该tid就是指向动态库中用户级线程控制结构体(struct pthread)的起始地址
线程栈
-
- 从上图看出,动态库里面还有一个线程栈,这是动态库中提供的线程私有栈结构,它是给新线程使用的栈结构
-
- 主线程的独立栈结构,用的是地址空间的栈区
-
- 也就是说:创建线程,用的是地址空间的栈区,而线程内部也维护了一个独立栈结构,仅线程使用,线程退出后,这个栈结构也会被释放
线程局部存储(TLS)
-
- 如果需要在一个线程内部的各个函数调用都能访问、但其它线程不能访问的变量(被称为线程局部静态变量),就需要新的机制来实现,这就是TLS
-
- 在主线程创建的全局变量或静态变量,都是所有线程共享的资源,我们不想让其他线程访问可以使用__thread修饰变量,也就是TLS机制
-
- 被__thread修饰后的变量,全部线程访问的都不是变量相同的地址,变量被不同线程访问时,都会拷贝一份新的变量给线程(地址不一样)
验证
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <pthread.h>
using namespace std;
__thread int global_value = 100;
void *CallPthread(void *argc)
{
cout << "name: " << (const char *)argc << ", &global_value: " << &global_value
<< ", thread id: " << pthread_self() << endl;
pthread_exit(nullptr);
}
int main()
{
// 1.定义线程ID
pthread_t tid1;
pthread_t tid2;
// 2.创建线程
if (pthread_create(&tid1, nullptr, CallPthread, (void *)"thread one") != 0)
{
exit(EXIT_FAILURE);
}
if (pthread_create(&tid2, nullptr, CallPthread, (void *)"thread two") != 0)
{
exit(EXIT_FAILURE);
}
// 3. 线程等待
pthread_join(tid1, nullptr);
pthread_join(tid2, nullptr);
return 0;
}
🍹1.3.2、获取线程ID
-
线程ID
-
前面所说的线程ID是LWP(轻量级进程ID),用于标识线程的唯一性
-
- pthread_ create函数会产生一个线程ID,存放在第一个参数指向的地址中。该线程ID和前面说的线程ID不是一回事
-
- 前面讲的线程ID属于进程调度的范畴。因为线程是轻量级进程,是操作系统调度器的最小单位,所以需要一个数值来唯一表示该线程
-
- pthread_ create函数第一个参数指向一个虚拟内存单元,该内存单元的地址即为新创建线程的线程ID,属于NPTL线程库的范畴
-
- 线程库的后续操作,就是根据该线程ID来操作线程的
线程库NPTL提供了pthread_ self函数,可以获得线程自身的ID:
#include <pthread.h>
typedef unsigned long int pthread_t;
pthread_t pthread_self(void);
-
函数解析
-
- 作用:获取线程自身的ID,在线程执行函数中获取
-
- 返回值:此函数一定会调用成功,返回调用线程的ID
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <pthread.h>
using namespace std;
void *CallPthread1(void *arg)
{
// 获取线程id
pthread_t id = pthread_self();
printf("I am %s, pthread ID: %u\n", (const char *)arg, id);
// 指定线程退出状态值
return (void *)0;
}
void *CallPthread2(void *arg)
{
// 获取线程id
pthread_t id = pthread_self();
printf("I am %s, pthread ID: %u\n", (const char *)arg, id);
// 指定线程退出状态值
return (void *)0;
}
int main()
{
// 定义线程ID,该id由OS填充,该id不是LWP,而是真线程ID
pthread_t tid1;
pthread_t tid2;
// 创建线程,并且填充t1的id,arg参数给线程命名,并且让线程执行CallPthread代码
if (pthread_create(&tid1, nullptr, CallPthread1, (void *)"thread t1") != 0)
{
exit(EXIT_FAILURE);
}
if (pthread_create(&tid2, nullptr, CallPthread2, (void *)"thread t2") != 0)
{
exit(EXIT_FAILURE);
}
return 0;
}
运行结果:
[lyh@192 Make_thread(1)]$ ./Thread_Creation
I am thread t1, pthread ID: 3796985600
I am thread t2, pthread ID: 3788592896
🍨1.4、线程终止(pthread_exit/cancel)
如果需要只终止某个线程而不终止整个进程
-
三种方法
-
- 从线程函数return。这种方法对主线程不适用,从main函数return相当于调用exit
-
- 线程可以调用pthread_ exit函数终止自己
-
- 一个线程可以调用pthread_ cancel函数终止同一进程中的另一个线程
pthread_exit函数:
#include <pthread.h>
typedef unsigned long int pthread_t;
void pthread_exit(void *value_ptr);
-
函数解析
-
- 作用:该函数终止正在运行的线程,就如同进程在结束时调用exit函数一样
-
- 返回值:无返回值,此函数一定会调用成功
-
- value_ptr:保存线程退出后的返回值,返回一个指向某个对象的指针,该指针不能是线程栈局部变量,通过线程等待可以获取这个变量
- 需要注意:pthread_exit或者return返回的指针所指向的内存单元必须是全局的或者是用malloc分配的,不能在线程函数的栈上分配,因为当其它线程得到这个返回指针时线程函数已经退出了
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <pthread.h>
using namespace std;
void *CallPthread1(void *arg)
{
// 获取线程id
pthread_t id = pthread_self();
printf("I am %s, pthread ID: %u\n", (const char *)arg, id);
// 线程终止,线程退出后的值设为空 -- 暂时忽略线程返回值
pthread_exit(nullptr);
}
void *CallPthread2(void *arg)
{
// 获取线程id
pthread_t id = pthread_self();
printf("I am %s, pthread ID: %u\n", (const char *)arg, id);
pthread_exit(nullptr);
}
int main()
{
// 定义线程id,该id由OS填充,该id不是LWP,而是真线程ID
pthread_t tid1;
pthread_t tid2;
// 创建线程,并且填充t1的id,arg参数给线程命名,并且让线程执行CallPthread代码
if (pthread_create(&tid1, nullptr, CallPthread1, (void *)"thread t1") != 0)
{
exit(EXIT_FAILURE);
}
if (pthread_create(&tid2, nullptr, CallPthread2, (void *)"thread t2") != 0)
{
exit(EXIT_FAILURE);
}
return 0;
}
pthread_cancel函数:
#include <pthread.h>
typedef unsigned long int pthread_t;
int pthread_cancel(pthread_t thread);
-
函数解析
-
- 作用:pthread_cancel函数向进程中正在运行的线程发送取消请求
-
- 返回值:成功返回0,失败返回一个errno错误码
-
- thread:线程ID(pthread_create中第一个输出型参数的值)
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <pthread.h>
using namespace std;
void *CallPthread1(void *arg)
{
pthread_t id = pthread_self();
while (true)
{
printf("I am %s, pthread ID: %u\n", (const char *)arg, id);
sleep(1);
}
pthread_exit(nullptr);
}
void *CallPthread2(void *arg)
{
pthread_t id = pthread_self();
while (true)
{
printf("I am %s, pthread ID: %u\n\n", (const char *)arg, id);
sleep(1);
}
pthread_exit(nullptr);
}
int main()
{
pthread_t tid1;
pthread_t tid2;
// 创建线程,并且填充t1的id,arg参数给线程命名,并且让线程执行CallPthread代码
if (pthread_create(&tid1, nullptr, CallPthread1, (void *)"thread t1") != 0)
{
exit(EXIT_FAILURE);
}
if (pthread_create(&tid2, nullptr, CallPthread2, (void *)"thread t2") != 0)
{
exit(EXIT_FAILURE);
}
// 二秒后退出线程ID为tid1的线程
sleep(2);
pthread_cancel(tid1);
return 0;
}
运行结果:
🍨1.5、线程等待(pthread_join)
-
为什么需要线程等待?
-
- 已经退出的线程,其空间没有被释放,仍然在进程的地址空间内
-
- 创建新的线程不会复用刚才退出线程的地址空间
#include <pthread.h>
typedef unsigned long int pthread_t;
int pthread_join(pthread_t thread, void **value_ptr);
-
函数解析
-
- 作用:pthread_join函数等待指定的线程终止,如果该线程已经终止,则pthread_join()立即返回
-
- 返回值:成功返回0,失败返回一个errno错误码
-
- thread:线程ID(pthread_create中第一个输出型参数的值)
-
- value_ptr:它指向一个指针,这个指针(解引用value_ptr)指向线程的返回值,它是一个输出型参数
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <pthread.h>
using namespace std;
void *CallPthread1(void *arg)
{
pthread_t id = pthread_self();
printf("I am %s, pthread ID: %u\n", (const char *)arg, id);
// p是一个线程状态返回值,传给pthread_exit参数
const char* p = "线程退出成功1! ! !";
pthread_exit((void*)p);
}
void *CallPthread2(void *arg)
{
pthread_t id = pthread_self();
printf("I am %s, pthread ID: %u\n", (const char *)arg, id);
const char* p = "线程退出成功2! ! !";
pthread_exit((void*)p);
}
int main()
{
// 定义线程id,该id由OS填充
pthread_t tid1;
pthread_t tid2;
// 创建线程,并且填充t1的id,arg参数给线程命名,并且让线程执行CallPthread代码
if (pthread_create(&tid1, nullptr, CallPthread1, (void *)"thread t1") != 0)
{
exit(EXIT_FAILURE);
}
if (pthread_create(&tid2, nullptr, CallPthread2, (void *)"thread t2") != 0)
{
exit(EXIT_FAILURE);
}
// 等待线程退出,并且获取线程退出时的返回值
void* RV1;
void* RV2;
pthread_join(tid1, &RV1);
pthread_join(tid2, &RV2);
cout << (const char*)RV1 << endl;
cout << (const char*)RV2 << endl;
return 0;
}
运行结果:
[lyh@192 Make_thread(1)]$ make
g++ -o Thread_Creation Thread_Creation.cpp -std=c++11 -lpthread
[lyh@192 Make_thread(1)]$ ./Thread_Creation
I am thread t1, pthread ID: 2257434368
I am thread t2, pthread ID: 2249041664
线程退出成功1! ! !
线程退出成功2! ! !
代码解析:
-
调用该函数的进程将挂起等待,直到线程id为thread的线程终止
-
thread以不同的方法终止,通pthread_join得到的终止状态是不同的
-
总结
-
- 如果thread通过return返回,value_ ptr所指向的内存单元里存放的是thread线程函数的返回值
-
- 如果thread被别的线程调用pthread_ cancel异常终止,value_ ptr所指向的单元里存放的是v常数PTHREAD_ CANCELED
-
- 如果thread是自己调用pthread_exit终止的,value_ptr所指向的单元存放的是传给pthread_exit的参数
-
- 如果对thread的终止状态不感兴趣,可以传nullpyt/NULL给value_ ptr参数
-
- 线程的返回值,只要是全局变量或堆空间的值,在全部线程中都是可见的
🍱1.6、分离线程(pthread_detach)
-
概念
-
- 默认情况下,新创建的线程是joinable(被等待)的,线程退出后,需要对其进行pthread_join操作,否则无法释放资源,从而造成系统泄漏
-
- 如果不关心线程的返回值,join是一种负担,这个时候,我们可以告诉系统,当线程退出时,自动释放线程资源
#include <pthread.h>
typedef unsigned long int pthread_t;
int pthread_detach(pthread_t thread);
-
函数解析
-
- 作用:该函数可以让主线程和线程进行分离,线程自动释放资源
-
- 返回值:成功返回0,失败返回一个errno错误码
-
- thread:线程ID(pthread_create中第一个输出型参数的值)
注意
-
- 可以是线程组内其他线程对目标线程进行分离,也可以是线程自己分离
-
- joinable(被等待)和分离是冲突的,一个线程不能既是joinable又是分离的
-
- 新线程分离后,主线程先退出,线程的资源也会被释放,线程是主线程的执行流
验证:在线程内部进行分离
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <pthread.h>
using namespace std;
void PrintThreadID(pthread_t id)
{
printf("thread id: #%x\n", id);
}
void* CallPthread(void* argc)
{
// 分离线程
pthread_detach(pthread_self());
cout << "name: " << (const char*)argc;
PrintThreadID(pthread_self());
// 线程终止
pthread_exit(nullptr);
}
int main()
{
// 1.定义线程ID
pthread_t tid1;
pthread_t tid2;
// 2.创建线程
if(pthread_create(&tid1, nullptr, CallPthread, (void*)"thread one") != 0)
{
exit(EXIT_FAILURE);
}
if(pthread_create(&tid2, nullptr, CallPthread, (void*)"thread two") != 0)
{
exit(EXIT_FAILURE);
}
sleep(1);
// 3. 线程等待
int n = pthread_join(tid1, nullptr);
cout << "pthread_join error: " << strerror(n) << endl;
n = pthread_join(tid2, nullptr);
cout << "pthread_join error: " << strerror(n) << endl;
return 0;
}
运行结果:
[lyh@192 Thread_detach(2)]$ ./Detach
name: thread twothread id: #de716700
name: thread onethread id: #def17700
pthread_join error: Invalid argument
pthread_join error: Invalid argument
为什么要在创建线程后休眠一秒呢?
-
因为主线程和线程被CPU调度是无序的,可能先执行完线程函数,主线程再等待
-
也可能是线程执行函数还没开始执行,主线程就已经开始挂起等待(pthread_join)了
验证:在主线程内分离指定线程(建议这样用)
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <pthread.h>
using namespace std;
void PrintThreadID(pthread_t id)
{
printf("thread id: #%x\n", id);
}
void* CallPthread(void* argc)
{
cout << "name: " << (const char*)argc;
PrintThreadID(pthread_self());
// 线程终止
pthread_exit(nullptr);
}
int main()
{
// 1.定义线程ID
pthread_t tid1;
pthread_t tid2;
// 2.创建线程
if(pthread_create(&tid1, nullptr, CallPthread, (void*)"thread one") != 0)
{
exit(EXIT_FAILURE);
}
if(pthread_create(&tid2, nullptr, CallPthread, (void*)"thread two") != 0)
{
exit(EXIT_FAILURE);
}
sleep(1);
// 分离指定的线程
pthread_detach(tid1);
pthread_detach(tid2);
// 3. 线程等待
int n = pthread_join(tid1, nullptr);
cout << "pthread_join error: " << strerror(n) << endl;
n = pthread_join(tid2, nullptr);
cout << "pthread_join error: " << strerror(n) << endl;
return 0;
}
运行结果:
[lyh@192 Thread_detach(2)]$ make
g++ -o Detach Detach.cpp -std=c++11 -lpthread
[lyh@192 Thread_detach(2)]$ ./Detach
name: thread onethread id: #4e183700
name: thread twothread id: #4d982700
pthread_join error: Invalid argument
pthread_join error: Invalid argument
-
结论
-
- 在分离线程后,对应的主线程一般不要退出(常驻内存的进程)
-
- 分离线程是线程的第四种退出方式:延后退出
🌸2、线程异常
🍡1.1、概念
主线程和线程(执行流)终止的方法有三种:
-
代码跑完,结果正确
-
代码跑完,结果不正确
-
程序异常(除零错误、越界访问等等)
-
延后退出(仅限线程,线程分离)
线程异常了怎么办呢?
-
整个进程整体异常退出,线程异常等于进程异常
-
线程会影响主线程(main)及其他线程的运行,体现出了线程的健壮性低,鲁棒性低
-
pthread_join第二个参数只能获取线程退出码,不会获取退出信号
-
因为线程没有退出信号,线程异常等于进程异常,只有进程收到信号,并且终止程序
🍢1.2、验证
线程执行程序写一个异常错误,然后验证OS是否发生信号
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <pthread.h>
using namespace std;
void PrintThreadID(pthread_t id)
{
printf("thread id: #%x\n", id);
}
void* CallPthread(void* argc)
{
cout << "name: " << (const char*)argc;
PrintThreadID(pthread_self());
// 除零错误
int a = 1, b = a / 0;
// 线程终止
pthread_exit(nullptr);
}
int main()
{
// 1.定义线程ID
pthread_t tid;
// 2.创建线程
if(pthread_create(&tid, nullptr, CallPthread, (void*)"thread one") != 0)
{
exit(EXIT_FAILURE);
}
// 3. 线程等待
pthread_join(tid, nullptr);
return 0;
}
运行结果:
[lyh@192 Thread_detach(2)]$ ./Detach
name: thread onethread id: #337b2700
Floating point exception (core dumped)