【Linux 20】线程控制

news2024/9/20 18:20:42

文章目录

  • 🌈 一、创建线程
    • ⭐ 1. 线程创建函数
    • ⭐ 2. 创建单线程
    • ⭐ 3. 给线程传参
    • ⭐ 4. 创建多线程
    • ⭐ 5. 获取线程 ID
  • 🌈 二、终止线程
    • ⭐1. 使用 return 终止线程
    • ⭐ 2. 使用 pthread_exit 函数终止线程
    • ⭐ 3. 使用 pthread_cancel 函数终止线程
  • 🌈 三、等待线程
    • ⭐ 1. 线程等待函数
    • ⭐ 2. 获取返回值
  • 🌈 四、分离线程
    • ⭐ 1. 线程分离概念
    • ⭐ 2. 线程分离函数

  • 引入线程库:要使用线程相关的函数,需要使用 #include <pthread.h> 引入原生线程库。
  • 链接线程库:引入线程库后,在编译程序时还需要使用 -lpthread 选项链接原生线程库。

🌈 一、创建线程

⭐ 1. 线程创建函数

  • 线程创建成功时返回 0,创建失败时返回错误码。
#include <pthread.h>

int pthread_create(
    	pthread_t *thread, 					/* 输出型参数,用以获取创建成功的线程 ID */
    	const pthread_attr_t *attr,			/* 设置创建线程的属性,使用 nullptr 表示使用默认属性 */
    	void *(*start_routine) (void *), 	/* 函数地址,该线程启动后需要去执行的函数 */
    	void *arg);							/* 线程要去执行的函数的形参,没参数时可填写 nullptr */

⭐ 2. 创建单线程

  • 当一个程序启动时,其中至少有一个线程在工作,这个线程就叫做主线程。
  • 主线程是产生其他子线程的线程,通常主线程必须最后完成某些执行操作。
  • 让主线程调用 pthread_create 函数创建一个新线程,然后让新线程跑去执行 thread_run 函数,而主线程则继续执行后续代码。
#include <unistd.h>
#include <iostream>
#include <pthread.h>

using std::cout;
using std::endl;

// 新线程
void* thread_run(void* args)
{
    while (true)
    {
        cout << "新 线程正在运行" << endl;
        sleep(1);
    }
}

// 主线程
int main()
{
    // 记录线程 id
    pthread_t tid;

    // 创建新线程,并让新线程去执行 thread_run 函数
    pthread_create(&tid, nullptr, thread_run, nullptr);
	
    // 主线程继续执行自己后续的代码
    while (true)
    {
        cout << "主 线程正在运行" << endl;
        sleep(1);
    }

    return 0;
}
  • 运行代码后会发现,主线程和新线程会交替执行自己的任务。

  • 在程序运行过程中,新开一个窗口使用ps axj命令查看当前进程的信息时,由于这两个线程都属于同一个进程,因此只能看到一个进程的信息。

image-20240912174356270

  • 如果想要查看线程 (轻量级进程) 的相关信息,可以使用 ps -aL 查看指定进程内部的轻量级进程的信息。
    • LWP (Light Weight Process) 表示的就是线程 (轻量级进程) 的 ID,其中主线程的 LWP 值等同于进程的 PID 值。

image-20240912175003416

⭐ 3. 给线程传参

  • 由于 pthread_create 函数创建出来的线程所执行的函数的形参类型是 void* 的,可以传递任何类型的参数,也能传对象给线程使用
#include <unistd.h>
#include <iostream>
#include <pthread.h>

using std::cout;
using std::endl;

class thread_data
{
public:
    thread_data(const string& thread_name)
        :_thread_name(thread_name)
        {}

    string thread_name()
    {
        return _thread_name;
    }

private:    
    string _thread_name;
};

void* thread_run(void* args)
{
    thread_data* td = static_cast<thread_data*>(args);

    while (true)
    {
        cout << "新线程 " << td->thread_name() << " 正在运行" << endl;
        sleep(1);
    }
}

int main()
{
    thread_data* td = new thread_data("thread 1");

    // 创建新线程,将 td 对象作为 thread_tun 函数的参数
    pthread_t tid;
    pthread_create(&tid, nullptr, thread_run, td);

    while (true)
    {
        cout << "主线程正在运行" << endl;
        sleep(1);
    }

    return 0;
}

⭐ 4. 创建多线程

  • 每个线程都由各自的线程 ID 所管理着,创建多线程时可以使用一个数据结构将每个创建出来的线程的 ID 管理起来。

举个例子

  • 创建 5 个线程,使用数组将这些线程的线程 ID 管理起来,让这些线程都去执行 thread_run 函数。
#include <string>
#include <vector>
#include <unistd.h>
#include <iostream>
#include <pthread.h>

using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::to_string;

class thread_data
{
public:
    thread_data(const string& thread_name)
        :_thread_name(thread_name)
        {}

    string thread_name()
    {
        return _thread_name;
    }

private:    
    string _thread_name;
};

void* thread_run(void* args)
{
    thread_data* td = static_cast<thread_data*>(args);

    while (true)
    {
        cout << "新线程 " << td->thread_name() << " 正在运行" << endl;
        sleep(1);
    }
}

const int thread_num = 5;   // 创建 5 个线程

int main()
{
    // 存储创建的所有线程的线程 ID
    vector<pthread_t> tids(thread_num);

    // 创建 5 个线程并给每个线程传递一个 thread_data 对象
    for (size_t i = 0; i < thread_num; i++)
    {
        string thread_name = "thread " + to_string(i + 1);
        thread_data* td = new thread_data(thread_name);
        pthread_create(&tids[i], nullptr, thread_run, td);
    }

    while (true)
    {
        cout << "主线程正在运行" << endl;
        sleep(1);
    }

    return 0;
}

  • 再使用 ps -aL 指令可以查到 主 + 新 这 6 个线程的情况。

image-20240912200440834

⭐ 5. 获取线程 ID

  1. 可通过 pthread_create 函数的第一个输出型参数获得创建的线程的 ID。
  2. 让线程执行的函数调用 pthread_self 函数获取调用该函数的线程的 ID。
    • 哪个线程调用该函数就返回哪个线程的线程 ID。
#include <pthread.h>

pthread_t pthread_self(void);
  • 注:pthread_self 函数获得的线程 ID 不等于内核的 LWP 值,pthread_self 函数获得的是用户级原生线程库的线程 ID,而 LWP 是内核的轻量级进程ID,它们之间是一对一的关系。

🌈 二、终止线程

  • 如果只是想终止某个线程而不是整个进程,可以有如下 3 种方法。
  1. 使用 return 终止线程:非主线程可以在执行的函数中使用 return 终止当前线程。
  2. 使用 pthread_exit 终止线程:线程自己可以调用该函数终止自己。
  3. 使用 pthread_cancel 终止线程:该函数能通线程 ID 终止任意线程。

⭐1. 使用 return 终止线程

  • 在线程调用的函数中使用 return 退出当前线程,但是在main函数中使用return代表整个进程退出,也就是说只要主线程退出了那么整个进程就退出了,此时该进程曾经申请的资源就会被释放,而其他线程会因为没有了资源,自然而然的也退出了。

举个例子

#include <string>
#include <unistd.h>
#include <iostream>
#include <pthread.h>

using std::cout;
using std::endl;
using std::string;

void* thread_run(void* args)
{
    string thread_name = static_cast<const char*>(args);

    // 让新线程运行 5 秒之后退出
    for (size_t i = 0; i < 5; i++)
    {
        cout << thread_name << " 正在运行" << endl;
        sleep(1);
    }

    return nullptr;
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, thread_run, (void*)"thread 1");

    while (true)
    {
        cout << "主线程正在运行" << endl;
        sleep(1);
    }

    return 0;
}

⭐ 2. 使用 pthread_exit 函数终止线程

#include <pthread.h>

void pthread_exit(void *retval);	// retval 是线程在退出时,线程所调用函数的返回值
  • pthread_exit 或者 return 返回的指针所指向的内存单元必须是全局的或者是用malloc 分配的,不能在线程函数的栈上分配,因为当其他线程得到这个返回指针时,线程函数已经退出了。

举个例子

#include <string>
#include <unistd.h>
#include <iostream>
#include <pthread.h>

using std::cout;
using std::endl;
using std::string;

void* thread_run(void* args)
{
    string thread_name = static_cast<const char*>(args);

    // 让新线程运行 5 秒之后退出
    for (size_t i = 0; i < 5; i++)
    {
        cout << thread_name << " 正在运行" << endl;
        sleep(1);
    }

    pthread_exit(nullptr);
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, thread_run, (void*)"thread 1");

    while (true)
    {
        cout << "主线程正在运行" << endl;
        sleep(1);
    }

    return 0;
}

⭐ 3. 使用 pthread_cancel 函数终止线程

  • 该函数可以根据线程 ID 终止任意线程,可以用来终止自己或其他线程,不过一般建议是拿来终止其他线程。

举个例子

  • 在新线程运行 5s 后,用主线程将新线程终止。
#include <string>
#include <unistd.h>
#include <iostream>
#include <pthread.h>

using std::cout;
using std::endl;
using std::string;

void* thread_run(void* args)
{
    string thread_name = static_cast<const char*>(args);

    while (true)
    {
        cout << thread_name << " 正在运行" << endl;
        sleep(1);
    }
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, thread_run, (void*)"thread 1");

    cout << "主线程正在运行" << endl;
    sleep(5);
    pthread_cancel(tid);    // 主线程终止新线程
    cout << "新线程已被终止" << endl;

    return 0;
}

image-20240914154513668

🌈 三、等待线程

  • 和进程等待一样,一个线程在被创建出来时,也需要被等待。
  • 新线程在退出时,主线程也要回收新线程所持有的资源,否则会产生类似进程的 “僵尸” 问题,从而造成内存泄漏。
  • 新线程退出时,主线程需要根据新线程的返回值,获取新线程的退出信息。
  • 此外,主线程不关心新线程异常退出的情况,因为线程一旦异常进程立马就会挂掉,只会关心进程退出的相关信息。

⭐ 1. 线程等待函数

  • 该函数在等待成功时会返回 0,失败时返回对应的错误码。
#include <pthread.h>

int pthread_join(
    	pthread_t thread,	/* 被等待的线程的线程 ID */ 
    	void **retval);		/* 获取被等待的线程在退出时的返回值 */
  • 调用该函数的线程将阻塞等待新线程,直到所等待的的新线程终止为止,被等待的线程以不同的方式终止时,通过 pthread_join 得到的终止状态也是不同的。

举个例子

#include <string>
#include <unistd.h>
#include <iostream>
#include <pthread.h>

using std::cout;
using std::endl;
using std::string;

void* thread_run(void* args)
{
    string thread_name = static_cast<const char*>(args);

    // 让新线程运行 5 秒之后退出
    for (size_t i = 0; i < 5; i++)
    {
        cout << thread_name << " 正在运行" << endl;
        sleep(1);
    }

    return nullptr;
}

int main()
{
    // 主线程创建新线程
    pthread_t tid;
    pthread_create(&tid, nullptr, thread_run, (void*)"thread 1");

    cout << "主线程正在运行" << endl;

    // 主线程阻塞等待 tid 所描述的新线程
    int n = pthread_join(tid, nullptr);

    cout << "主线程运行完毕, n: " << n << endl;

    return 0;
}

⭐ 2. 获取返回值

  • pthread_create 创建出的线程所调用函数的返回值是一个 void* 类型的值,而 pthread_join 函数的第二个输出型参数是 void** 类型,就是为了获取新线程所调用函数的返回值。

1. 举个例子

  • 让新线程调用的函数返回一个字符串。
#include <string>
#include <unistd.h>
#include <iostream>
#include <pthread.h>

using std::cout;
using std::endl;
using std::string;

void* thread_run(void* args)
{
    string thread_name = static_cast<const char*>(args);

    // 让新线程运行 5 秒之后退出
    for (size_t i = 0; i < 5; i++)
    {
        cout << thread_name << " 正在运行" << endl;
        sleep(1);
    }

    return (void*)"thread 1 done";  			// 返回这个字符串常量的起始地址
    // pthread_exit((void*)"thread 1 done");	// 这两种方法返回的内容一样
}

int main()
{
    // 主线程创建新线程
    pthread_t tid;
    pthread_create(&tid, nullptr, thread_run, (void*)"thread 1");

    cout << "主线程正在运行" << endl;

    // 主线程阻塞等待 tid 所描述的新线程
    void* ret = nullptr;            // 用来接收线程退出时的退出信息
    int n = pthread_join(tid, &ret);

    cout << "主线程运行完毕, n: " << n << endl;
    cout << "主线程获取到的新线程的退出信息为: " << (char*)ret << endl;

    return 0;
}

2. 线程退出时可以返回对象

  • 由于线程所调用的函数的返回值是 void* 类型的,意味着线程在完事之后还可以返回一个处理好的对象。
#include <string>
#include <unistd.h>
#include <iostream>
#include <pthread.h>

using std::cout;
using std::endl;
using std::string;

// 保存线程退出时的相关信息
class thread_return
{
public:
    pthread_t _id;   // 哪个线程退了
    string _info;    // 退出时想传达的信息
    int _code;       // 线程退出的信息
public:
    thread_return(pthread_t id, const string& info, int code)
        : _id(id), _info(info), _code(code)
    {}
};

// 新线程所执行的函数
void* thread_run(void* args)
{
    string thread_name = static_cast<const char*>(args);

    // 让新线程运行 5 秒之后退出
    for (size_t i = 0; i < 5; i++)
    {
        cout << thread_name << " 正在运行" << endl;
        sleep(1);
    }

    thread_return* ret = new thread_return(pthread_self(), "线程正常退出", 10);
    pthread_exit(ret);
}

int main()
{
    // 主线程创建新线程
    pthread_t tid;
    pthread_create(&tid, nullptr, thread_run, (void*)"thread 1");

    cout << "主线程正在运行" << endl;

    // 主线程阻塞等待 tid 所描述的新线程
    void* ret = nullptr;            // 用来接收线程退出时的退出信息
    int n = pthread_join(tid, &ret);
    cout << "主线程运行完毕, n: " << n << endl;

    thread_return* r = static_cast<thread_return*>(ret);

    cout << "主线程获取到的新线程的退出信息为" << endl;
    cout << "id: " << r->_id << ", info: " << r->_info << ", code: " << r->_code << endl;

    delete r;
    
    return 0;
}

image-20240914153537503

  • 这种特性使得我们可以将任务分配给线程运行,运行完毕之后再将结果返回。

🌈 四、分离线程

⭐ 1. 线程分离概念

  • 一般情况下,主线程是需要阻塞等待新线程的,如果不等待线程退出,就有可能造成 “僵尸” 问题。
  • 但如果主线程就是不想等待新线程退出,也不关心新线程的返回值,此时就可以将新线程分离,被分离的线程在退出时会自动释放资源
  • 分离出的新线程依旧要使用进程的资源,且依旧在该进程内运行,甚至这个新线程崩溃了也会影响整个进程。线程分离只是让主线程不需要再阻塞等待整个新线程罢了,在新线程退出时系统会自动回收该线程所持有的资源。
  • 一个线程可以将其他线程分离出去,也可以将自己分离出去。
  • 等待和分离会发生冲突,一个线程不能既是可被等待的又是分离出去的。
  • 虽然分离出去的线程已经不归主线程管了,但一般还是建议让主线程最后再退出
  • 分离出去的线程可以被 pthread_cancel 函数终止,但不能被 pthread_join 函数等待。

⭐ 2. 线程分离函数

#include <pthread.h>

int pthread_detach(pthread_t thread);	// thread 是要分离出去的线程的 ID
  • 线程分离成功时返回 0,失败时则返回对应错误码。

举个例子

  • 主线程将创建好的 thread 1 线程分离出去,之后主线程就不需要再 join 新线程了。
#include <string>
#include <unistd.h>
#include <iostream>
#include <pthread.h>

using std::cout;
using std::endl;
using std::string;

const int num = 5;

// 新线程
void* thread_run(void* args)
{
    string thread_name = static_cast<const char*>(args);
    
    // pthread_detach(tid); // 新线程也可以将自己分离出去

    for (int i = 0; i < num; i++)
    {
        cout << thread_name << " 正在运行" << endl;
        sleep(1);
    }

    pthread_exit(nullptr);
}

int main()
{
    // 创建新线程
    pthread_t tid;
    pthread_create(&tid, nullptr, thread_run, (void*)"thread 1");

    pthread_detach(tid); // 主线程将目标进程分离出去

    while (true)
    {
        cout << "I am a main thread" << endl;
        sleep(1);
    }

    return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2149656.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Python中lambda表达式的使用——完整通透版

文章目录 一、前言二、 基本语法三、举个简单的例子&#xff1a;四、常见应用场景1. 用于排序函数sort() 方法简介lambda 表达式的作用详细解释进一步扩展总结 2、与 map、filter、reduce 等函数结合1、 map() 函数示例&#xff1a;将列表中的每个数字平方 2、 filter() 函数示…

Centos 7 搭建Samba

笔记&#xff1a; 环境&#xff1a;VMware Centos 7&#xff08;网络请选择桥接模式&#xff0c;不要用NAT&#xff09; 遇到一个问题就是yum 安装404&#xff0c;解决办法在下面&#xff08;没有遇到可以无视这句话&#xff09; # 安装Samba软件 yum -y install samba# 创建…

Shader Graph Create Node---Channel

二、Channel 1、Combine(合并通道) 2、Flip(翻转) 3、Split(分离) 4、Swizzle(交换)

ELK环境部署

目录 环境准备 Elasticsearch 部署 安装Elasticsearch Elasticsearch-head 插件 安装node 安装 phantomjs 安装 Elasticsearch-head Logstash 安装部署 Kibana 安装部署 ELFK 本章纯搭建过程&#xff0c;几乎无任何注释解释 环境准备 ELK的搭建和测试&#xff0c;…

力扣(LeetCode)每日一题 2576. 求出最多标记下标

题目链接https://leetcode.cn/problems/find-the-maximum-number-of-marked-indices/description/?envTypedaily-question&envId2024-09-12 思路&#xff1a; 先排序&#xff0c;然后定义双指针 left&#xff0c;right&#xff0c;贪心遍历&#xff0c;左指针在中间&…

机器狗与无人机空地协调技术分析

随着科技的飞速发展&#xff0c;机器狗与无人机作为智能机器人领域的杰出代表&#xff0c;正逐步在军事侦察、灾害救援、环境监测、农业植保等多个领域展现出巨大的应用潜力。本文旨在深入探讨机器狗与无人机之间的空地协调技术&#xff0c;分析其在复杂环境中的协同作业机制、…

轻松打造:用Python实现手机与电脑间的简易消息系统

展示&#x1f3a5; 观看视频&#xff1a;&#x1f440;&#xff0c;这是之前完成的一个项目&#xff0c;但今天我们的重点不是这个哦。 告别往昔&#xff0c;启航新篇章 现象&#x1f31f; 智能互动&#xff1a;&#x1f4f1; 我们每天都在享受与智能设备的互动&#xff0c;…

作为HR,如何考察候选人的沟通能力

如何考察候选人的沟通能力。沟通能力&#xff0c;这个听起来简单&#xff0c;实际上却是一个非常复杂的技能&#xff0c;它关乎到一个人能否有效地传递信息&#xff0c;理解他人&#xff0c;并且在团队中发挥积极的作用。 作为HR&#xff0c;我们应该怎样才能精准地把握住候选…

鸿蒙开发(HarmonyOS)组件化浅谈

众所周知&#xff0c;现在组件化在移动开发中是很常见的&#xff0c;那么组件化有哪些好处&#xff1a; 1. 提高代码复用性&#xff1a;组件化允许将应用程序的不同功能模块化&#xff0c;使得这些模块可以在不同的项目中重复使用&#xff0c;从而提高开发效率并减少重复工作。…

JAVA并发编程系列(9)CyclicBarrier循环屏障原理分析

拼多多2面&#xff0c;还是模拟拼团&#xff0c;要求用户拼团成功后&#xff0c;提交订单支付金额。 之前我们在系列(8)《CountDownLatch核心原理》&#xff0c;实现过拼团场景。但是CountDownLatch里调用countDown()方法后&#xff0c;线程还是可以继续执行后面的代码&#xf…

【云安全】云上资产发现与信息收集

一、云基础设施组件 1、定义 在云计算基础架构中&#xff0c;基础设施组件通常包括&#xff1a;计算、存储、网络和安全等方面的资源。例如&#xff0c;计算资源可以是虚拟机、容器或无服务器计算引擎&#xff1b;存储资源可以是对象存储或块存储&#xff1b;网络资源可以是虚拟…

数字电路与逻辑设计-计数器逻辑功能测试

一&#xff0e;实验目的 1&#xff0e;验证用触发器构成的计数器计数原理&#xff1b; 2&#xff0e;掌握测试中规模集成计数器功能的方法&#xff1b; 二&#xff0e;实验原理 时序逻辑电路中&#xff0c;有一种电路称为计数器&#xff0c;计数器是用来对时钟脉冲进行计数的…

稳联Profinet转Canopen网关携手伺服,高效提升生产效率

在当今的工业生产领域&#xff0c;追求高效、精准和可靠的生产方式是企业不断努力的方向。稳联技术Profinet转Canopen&#xff08;WL-ABC3033&#xff09;网关与伺服系统的携手合作&#xff0c;为提高生产效率带来了新的机遇和突破。 实现无缝通信&#xff0c;优化生产流程稳联…

Flink提交任务

第3章 Flink部署 3.1 集群角色 3.2 Flink集群搭建 3.2.1 集群启动 0&#xff09;集群规划 表3-1 集群角色分配 具体安装部署步骤如下&#xff1a; 1&#xff09;下载并解压安装包 &#xff08;1&#xff09;下载安装包flink-1.17.0-bin-scala_2.12.tgz&#xff0c;将该jar包…

无人机之控制距离篇

无人机的控制距离是一个复杂且多变的概念&#xff0c;它受到多种因素的共同影响。以下是对无人机控制距离及其影响因素的详细分析&#xff1a; 一、无人机控制距离的定义 无人机控制距离指的是遥控器和接收机之间的最远传输距离。这个距离决定了无人机在操作者控制下能够飞行的…

2024年氧化工艺证考试题库及氧化工艺试题解析

题库来源&#xff1a;安全生产模拟考试一点通公众号小程序 2024年氧化工艺证考试题库及氧化工艺试题解析是安全生产模拟考试一点通结合&#xff08;安监局&#xff09;特种作业人员操作证考试大纲和&#xff08;质检局&#xff09;特种设备作业人员上岗证考试大纲随机出的氧化…

VM+Ubuntu16.04硬盘扩容

步骤&#xff1a; 用df -h查看自己虚拟机的硬盘空间使用情况在虚拟机下安装gparted软件备用 sudo apt-get install gparted在VM的界面或者Windows终端修改虚拟机硬盘大小回到虚拟机的gparted软件里&#xff0c;修改分区&#xff0c;先删除原有的逻辑分区和扩展分区&#xff0c…

一键快速替换PPT上的字体?这个你一定要学会。

前言 最近有个朋友在做PPT&#xff0c;说是准备在各大平台分发&#xff0c;咨询小白关于PPT上内容的事情&#xff0c;结果小白问了一句&#xff1a;字体用的是什么&#xff1f; 嗯……她说是&#xff1a;汉仪黑和字魂。 好家伙&#xff0c;这不是妥妥的当别人财神爷的机会吗&…

是的,第一个在Domino中整个AI本地大语言模型的开源项目已经发布

大家好&#xff0c;才是真的好。 做梦也没想到&#xff0c;上一篇《Notes&#xff0c;无代码应用开发王者归来&#xff01;》居然又一次焕发了卓越的青春活力&#xff0c;阅读量超高。真希望能再接再厉&#xff0c;续创辉煌。 但今天咱们怎么也不能再讲HCL Notes/Dmino 14.5 …

Vue3 : Pinia的性质与作用

目录 一.性质 二.作用 三.Pinia 的核心概念 四.使用 1.count.ts 2.count.vue Vue 3 中 Pinia 是一个专为 Vue 3 设计的状态管理库&#xff0c;它旨在提供一种简单、直观的方式来管理应用的状态。 一.性质 1.集成性&#xff1a;Pinia 是 Vue 3 官方推荐的状态管理库&…