linux:线程的控制

在这里插入图片描述

个人主页 : 个人主页
个人专栏 : 《数据结构》 《C语言》《C++》《Linux》

文章目录

  • 前言
  • 一、线程的总结
    • 1. 线程的优点
    • 2. 线程的缺点
    • 3. 线程异常
    • 4.线程和进程
  • 二、线程的控制
    • 创建线程
    • 线程终止
    • 线程等待
      • 获取返回值
    • 线程分离
  • 总结


前言

本文作为我对于线程的简单总结,线程控制的知识总结


一、线程的总结

1. 线程的优点

  • 创建一个新线程的代价比创建一个新进程小的多
  • 与进程之间的切换相比,线程之间的切换需要操作系统做的工作要小
  • 线程占有的资源要比进程少很多
  • 能充分利用多处理器的可并行数量(并行,多个执行流在同一时刻拿着不同的CPU继续运算,执行代码)
  • 在等待慢速I/O操作结束的同时,程序可执行其他的计算任务
  • 计算密集型应用,为了能在多处理器系统上运行,将计算分解到多个线程中实现
  • I/O密集型应用(如下载,上传),为了提高性能,将I/O操作重叠。线程可以同时等待不同的I/O操作

创建线程并不是越多越好,进程的执行效率,一定随着线程的数量增多,效率为正态分布的(线程的切换)。线程的个数最好 = CPU的个数 * CPU的核数


2. 线程的缺点

  • 性能损失:一个很少被外部事件阻塞的计算密集型线程往往无法与其他线程共享同一个处理器,如果计算密集型线程的数量比可用的处理器多,那么可能会有较大的性能损失,如增加了额外的同步和调度开销,而可用的资源不变
  • 健壮性(鲁棒性)降低:编写多线程需要全面更深入的考虑,在一个多线程程序里,因时间分配的细微偏差或者因共享了不该共享的变量而造成不良影响的可能性是很大的,也就是说线程之间是缺乏保护的
  • 缺乏访问控制:因为线程共享地址空间,那一个线程定义的局部变量 or new出的空间,其它线程也能使用,这就导致一个线程可能会不小心修改另一个线程正在使用的数据,导致数据不一致、逻辑错误甚至程序崩溃
  • 编程难度提高:编写与调试一个多线程程序比单线程程序困难

3. 线程异常

  • 单个线程如果出现除0,野指针问题导致线程崩溃,进程也会崩溃
  • 线程是进程的执行分支,线程出异常,就类似进程出异常,进而触发信号机制,终止进程,进程终止,该进程内的所有线程也全部终止

创建5个线程,其中线程thread-4触发除0异常。

#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <string>
#include <functional>
#include <time.h>
#include <vector>

using func_t = std::function<void()>;
const int threadnum = 5;

class ThreadDate
{
public:
    ThreadDate(const std::string &name, const uint64_t &time, func_t f)
        : threadname(name), createtime(time), func(f)
    {
    }

public:
    std::string threadname;
    uint64_t createtime;
    func_t func;
};

void Print()
{
    std::cout << "执行的任务..." << std::endl;
}

void *threadRoutine(void *args)
{
    ThreadDate *td = static_cast<ThreadDate *>(args);
    while (true)
    {
        std::cout << "new thread "
                  << " name: " << td->threadname << " create time: " << td->createtime << std::endl;
        td->func();

        if(td->threadname == "thread-4")
        {
            std::cout << td->threadname << "触发异常" << std::endl;
            int a = 10;
            a /= 0;
        }
        sleep(1);
    }
}

int main()
{
    std::vector<pthread_t> pthreads;
    for (int i = 0; i < threadnum; ++i)
    {
        char threadname[64];
        snprintf(threadname, sizeof(threadname), "%s-%d", "thread", i);

        pthread_t tid;
        ThreadDate *td = new ThreadDate(threadname, (uint64_t)time(nullptr), Print);
        pthread_create(&tid, nullptr, threadRoutine, (void *)td);

        pthreads.push_back(tid);
        sleep(1);
    }

    while (true)
    {
        std::cout << "main thread" << std::endl;
        sleep(1);
    }

    return 0;
}

在这里插入图片描述
其中thread-4线程触发异常,进程直接终止。


4.线程和进程

  • 进程是资源分配的基本单位
  • 线程是调度的基本单位
  • 线程共享进程数据,但也有自己的一部分独立的数据(线程ID,线程的上下文数据独立的栈结构(pthread库维护),errno,信号屏蔽字,调度优先级)
  • 进程的多个线程共享同一个地址空间,因此地址空间的代码段,数据段都是共享的,线程还共享进程的文件描述符表,每种信号的处理方式(SIG_IGN,SIG_DFL,自定义的信号处理函数),当前工作目录(cwd),用户id和组id

进程和线程的关系如下:
在这里插入图片描述

二、线程的控制

创建线程

在这里插入图片描述

函数原型: int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
功能:创建一个新的线程
参数:thread:返回线程ID
attr:设置线程属性,attr为nullptr表示使用默认属性
start_routine:是函数指针,为线程启动后要执行的函数
arg:传给线程启动函数的参数
返回值:成功返回0,失败返回错误码
错误检查:传统的一些函数,成功返回0,失败返回-1,并且对全局变量errno赋值以指示错误。pthreads函数出错时不会设置全局变量errno,而是将错误码通过返回值返回。pthreads也同样提供线程内errno变量,以支持其它使用errno的代码。对于pthreads函数的错误,建议通过返回值的判定,因为读取返回值要比读取线程内的errno变量更方便。

下面我们要创建一个新线程循环打印"new thread’,主线程循环打印"main thread"

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

void *threadRoutine(void *args)
{
    while(true)
    {
        std::cout << "new thread" << std::endl;
        sleep(1);
    }
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, nullptr);

    while(true)
    {
        std::cout << "main thread" << std::endl;
        sleep(1);
    }

    return 0;
}

在这里插入图片描述
编译时会有报错,编译器不认识pthread_create这个函数,但我们不是加了头文件吗?
我们知道linux没有真正的线程,只有轻量级进程的概念,所以操作系统只会提供轻量级进程创建的系统调用,不会直接提供线程创建的接口。linux为了解决这一问题,就在软件层创建了pthread原生线程库对用户提供线程控制接口,在内部将线程与轻量级进程一一对应。

在这里插入图片描述

这样有什么好处?标准化和兼容性(pthread编写的多线程应用程序在遵循POSIX标准的各种Unix-like系统上(包括Linux)都具有很高的可移植性),灵活性:pthread库提供了一套丰富的API,用于线程的创建、管理、同步和通信。这使得开发者可以根据应用程序的需求灵活地创建和管理线程,实现复杂的并发和并行计算任务。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

结果如上。


如何给创建的线程传参?

在这里插入图片描述
如上,arg的类型是void*类型,表示任意类型的指针。也就是说,我们可以穿一个整形,字符串,结构体对象。如下传递结构体。

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

using func_t = std::function<void()>;

class ThreadDate
{
public:
    ThreadDate(const std::string &name, const uint64_t &time, func_t f)
    :threadname(name), createtime(time), func(f)
    {}
public:
    std::string threadname;
    uint64_t createtime;
    func_t func;
};

void Print()
{
    std::cout << "执行的任务..." << std::endl;
}

void *threadRoutine(void *args)
{
    ThreadDate * td = static_cast<ThreadDate*>(args);
    while(true)
    {
        std::cout << "new thread "<< " name: " << td->threadname << " create time: " << td->createtime<< std::endl;
        td->func();
        sleep(1);
    }
}

int main()
{
    pthread_t tid;
    ThreadDate * td = new ThreadDate("thread-1", (uint64_t)time(nullptr), Print);
    pthread_create(&tid, nullptr, threadRoutine, (void *)td);

    while(true)
    {
        std::cout << "main thread" << std::endl;
        sleep(1);
    }

    return 0;
}

在这里插入图片描述


那如何一次创建多个线程?与创建多个子进程类似。

#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <string>
#include <functional>
#include <time.h>
#include <vector>

using func_t = std::function<void()>;
const int threadnum = 5;

class ThreadDate
{
public:
    ThreadDate(const std::string &name, const uint64_t &time, func_t f)
        : threadname(name), createtime(time), func(f)
    {
    }

public:
    std::string threadname;
    uint64_t createtime;
    func_t func;
};

void Print()
{
    std::cout << "执行的任务..." << std::endl;
}

void *threadRoutine(void *args)
{
    ThreadDate *td = static_cast<ThreadDate *>(args);
    while (true)
    {
        std::cout << "new thread "
                  << " name: " << td->threadname << " create time: " << td->createtime << std::endl;
        td->func();
        sleep(1);
    }
}

int main()
{
    std::vector<pthread_t> pthreads;
    for (int i = 0; i < threadnum; ++i)
    {
        char threadname[64];
        snprintf(threadname, sizeof(threadname), "%s-%d", "thread", i);

        pthread_t tid;
        ThreadDate *td = new ThreadDate(threadname, (uint64_t)time(nullptr), Print);
        pthread_create(&tid, nullptr, threadRoutine, (void *)td);

        pthreads.push_back(tid);
        sleep(1);
    }

    while (true)
    {
        std::cout << "main thread" << std::endl;
        sleep(1);
    }

    return 0;
}

在这里插入图片描述

在这里插入图片描述


下面代码,创建线程并通过返回值打印其线程id。

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


void *threadRoutine(void *args)
{
    std::string name = static_cast<const char*>(args);
    while(true)
    {
        std::cout << "new thread, name: " << name << std::endl;
        sleep(1);
    }
}

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

    while(true)
    {
        std::cout << "main thread, sub thread: " << tid << std::endl;
        sleep(1);
    }
    return 0;
}

在这里插入图片描述
在这里插入图片描述

很明显,创建出来的线程id与LWP并不同,那主线程的id值是否也与LWP不同?这我们就要了解一个接口pthread_self(线程获取自己的id)
在这里插入图片描述

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


void *threadRoutine(void *args)
{
    std::string name = static_cast<const char*>(args);
    while(true)
    {
        std::cout << "new thread, name: " << name << "thread id is " << pthread_self() << std::endl;
        sleep(1);
    }
}

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

    while(true)
    {
        std::cout << "main thread id " << pthread_self() << "sub thread id " << tid << std::endl;
        sleep(1);
    }
    return 0;
}

在这里插入图片描述
在这里插入图片描述
主线程的id与新线程的id明显不是LWP,那是什么呢?我们以十六进制打印看看。

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

// 转换为十六进制
std::string ToHex(pthread_t tid)
{
    char id[64];
    snprintf(id, sizeof(id), "0x%lx", tid);

    return id;
}


void *threadRoutine(void *args)
{
    std::string name = static_cast<const char*>(args);
    while(true)
    {
        std::cout << "new thread, name: " << name << "thread id is " << ToHex(pthread_self()) << std::endl;
        sleep(1);
    }
}

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

    while(true)
    {
        std::cout << "main thread id " << ToHex(pthread_self()) << std::endl;
        sleep(1);
    }
    return 0;
}

在这里插入图片描述
现在,这个线程id是不是很像地址? 没错,线程id就是地址。


线程终止

如果需要只终止某个线程而不终止整个进程,可以有三个方法:

  • 从线程函数return。这种方法对主线程不适用,从main函数return相当于调用exit函数
#include <iostream>
#include <pthread.h>
#include <string>
#include <unistd.h>

// 转换为十六进制
std::string ToHex(pthread_t tid)
{
    char id[64];
    snprintf(id, sizeof(id), "0x%lx", tid);

    return id;
}


void *threadRoutine(void *args)
{
    std::string name = static_cast<const char*>(args);
    int cnt = 5;
    while(cnt--)
    {
        std::cout << "new thread, name: " << name << "thread id is " << ToHex(pthread_self()) << std::endl;
        sleep(1);
    }

    return nullptr; // 线程终止
}

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

    while(true)
    {
        std::cout << "main thread id " << ToHex(pthread_self()) << std::endl;
        sleep(1);
    }
    return 0;
}

在这里插入图片描述
在这里插入图片描述

  • 线程可以调用pthread_exit终止自己
    在这里插入图片描述
#include <iostream>
#include <pthread.h>
#include <string>
#include <unistd.h>

// 转换为十六进制
std::string ToHex(pthread_t tid)
{
    char id[64];
    snprintf(id, sizeof(id), "0x%lx", tid);

    return id;
}


void *threadRoutine(void *args)
{
    std::string name = static_cast<const char*>(args);
    int cnt = 5;
    while(cnt--)
    {
        std::cout << "new thread, name: " << name << "thread id is " << ToHex(pthread_self()) << std::endl;
        sleep(1);
    }

    //return nullptr; // 线程终止
    pthread_exit(nullptr);
}

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

    while(true)
    {
        std::cout << "main thread id " << ToHex(pthread_self()) << std::endl;
        sleep(1);
    }
    return 0;
}

在这里插入图片描述
在这里插入图片描述

  • 一个线程可以调用pthread_cancel终止同一进程中的另一个线程

在这里插入图片描述

  • 功能:取消一个执行中的线程
  • 参数 thread:线程id
  • 返回值:成功返回0,失败返回错误码

不能用exit函数来终止线程,这会导致这个进程被终止。

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

// 转换为十六进制
std::string ToHex(pthread_t tid)
{
    char id[64];
    snprintf(id, sizeof(id), "0x%lx", tid);

    return id;
}


void *threadRoutine(void *args)
{
    std::string name = static_cast<const char*>(args);
    int cnt = 5;
    while(cnt--)
    {
        std::cout << "new thread, name: " << name << "thread id is " << ToHex(pthread_self()) << std::endl;
        sleep(1);
    }

    //return nullptr; // 线程终止
    exit(13);
}

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

    while(true)
    {
        std::cout << "main thread id " << ToHex(pthread_self()) << std::endl;
        sleep(1);
    }
    return 0;
}

在这里插入图片描述
在这里插入图片描述


线程等待

获取返回值

我们知道子进程退出时会有僵尸状态,需要父进程来等待子进程。那线程也要被等待吗?是的,线程也要被等待。线程退出没有等待,也会导致类似进程的僵尸问题。线程通过等待获取新线程的返回值。
在这里插入图片描述

  • thread:线程id
  • retval:指向一个指针,后者指向返回值
  • 返回值:等待成功返回0,失败返回错误码
    调用该函数的线程将以阻塞的方式等待,直到id为thread的线程终止。thread线程以不同的方法终止,通过pthread_join的终止状态是不同的:
  • 如果thread线程通过return返回,retval所指向的单元存放的是thread线程函数的返回值
  • 如果thread线程被别的线程调用pthread_cancel异常终止,retval所指向的单元存放的是常数PTHREAD_CANCELED
  • 如果thread线程是自己调用pthread_exit终止的,retval所指向的单元存放的是传给pthread_exit的参数
  • 如果不想获取thread的返回值,可以将nullptr传给retval

以从线程return为例。

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

// 转换为十六进制
std::string ToHex(pthread_t tid)
{
    char id[64];
    snprintf(id, sizeof(id), "0x%lx", tid);

    return id;
}

void *threadRoutine(void *args)
{
    std::string name = static_cast<const char *>(args);
    int cnt = 5;
    while (cnt--)
    {
        std::cout << "new thread, name: " << name << "thread id is " << ToHex(pthread_self()) << std::endl;
        sleep(1);
    }

    return nullptr; // 线程终止
}

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

    std::cout << "main thread id " << ToHex(pthread_self()) << std::endl;
    
    int n = pthread_join(tid, nullptr);
    std::cout << "main thread done , n : " << n << std::endl;

    return 0;
}

在这里插入图片描述
在这里插入图片描述


线程分离

对于一个线程,如果主线程要等待它,则会阻塞等待导致主线程自己的任务效率低。如果主线程不等待它,则可能导致类似僵尸进程的状态,使资源泄漏。这时我们就可以将线程分离。
线程可以设置为分离属性,一个线程如果被设置为分离属性,则该线程在退出之后,不需要其它执行流回收该线程资源,而是由操作系统进行回收。

  • 默认情况下,新创建的线程是joinable的,线程退出后,需要对其进行pthread_join操作,否则无法释放资源,从而造成资源泄漏
  • 如果不关心线程的返回值,join是一种负担,我们可以告诉系统,当线程退出时,自动释放线程资源。
  • joinable和分离是冲突的,一个线程不能既是joinable又是分离的。如果等待一个分离的线程,函数会立即返回错误,错误码通常为EINVAL

在这里插入图片描述

  • thread:线程id
  • 返回值:成功返回0,失败返回错误码

分离的线程在同一个进程地址空间,相互的线程不会相互干扰,但是如果分离的线程崩溃,进程也会崩溃。

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

// 转换为十六进制
std::string ToHex(pthread_t tid)
{
    char id[64];
    snprintf(id, sizeof(id), "0x%lx", tid);

    return id;
}

void *threadRoutine(void *args)
{
    std::string name = static_cast<const char *>(args);
    int cnt = 5;
    while (cnt--)
    {
        std::cout << "new thread, name: " << name << "thread id is " << ToHex(pthread_self()) << std::endl;
        sleep(1);
    }

    int a = 10;
    a /= 0; // 除0异常

    return nullptr; // 线程终止
}

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

    std::cout << "main thread id " << ToHex(pthread_self()) << std::endl;
    sleep(10);
    pthread_cancel(tid);

    // int n = pthread_join(tid, nullptr);
    // std::cout << "main thread done , n : " << n << std::endl;

    return 0;
}

在这里插入图片描述
在这里插入图片描述


总结

以上就是我对于线程控制的总结。

在这里插入图片描述

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

git远程仓库使用

赋值这个地址clone 克隆之后 cd slam_oncloud/ git remote add chenxnew ssh://git192.168.3.40:1022/chenxiao/slam_oncloud.git 查看一下 linuxchenxiao:/media/linux/mydisk/cloud_slam/slam_oncloud$ git remote add chenxnew ssh://git192.168.3.40:1022/chenxiao/sla…

GitHub Desktop的常用操作【图形化】

文章目录 【1】仓库的创建和删除【2】文件操作【3】分支原理与分支操作1.分支创建2.分支合并 【4】标签 【1】仓库的创建和删除 在本地创建一个新的仓库&#xff1a; 然后输入仓库的名称&#xff0c;描述&#xff0c;并选择路径&#xff1a; 点击完后就发现我们的仓库创建好…

明日周刊-第1期

打算开一个新的专栏&#xff0c;专门记录一周发生的事情以及资源共享&#xff0c;那么就从第一期开始吧。 1. 一周热点 人工智能技术突破&#xff1a;可能会有关于人工智能领域的最新研究成果&#xff0c;例如新算法的开发、机器学习模型的提升或者AI在不同行业的应用案例。 量…

natfrp和FRP配置SSL的基本步骤和bug排查

获取免费/付费SSL 我直接买了一年的ssl证书 设置 主要参考&#xff1a;https://doc.natfrp.com/frpc/ssl.html 遇到的Bug root域名解析是ALIAS&#xff0c;不是CNAME不要用NATFRP &#xff08;SakuraFrp&#xff09;同步Joplin&#xff0c;会出现webdav错误导致大量笔记被…

鸿蒙Harmony应用开发—ArkTS声明式开发(基础手势:Gauge)

数据量规图表组件&#xff0c;用于将数据展示为环形图表。 说明&#xff1a; 该组件从API Version 8开始支持。后续版本如有新增内容&#xff0c;则采用上角标单独标记该内容的起始版本。 子组件 可以包含单个子组件。 说明&#xff1a; 建议使用文本组件构建当前数值文本和辅…

sql server 恢复数据库、恢复单表数据的方法

如果不小心把某个表的数据删了&#xff0c;可以用之前的备份文件对单表进行数据恢复。 1、新建一个数据库&#xff08;全新的数据库&#xff09;&#xff0c;记得路径&#xff0c;恢复的时候要用到&#xff0c;新建完不要对数据库做什么操作。 2、用需要恢复表的数据库的备份文…

【leetcode热题】排序链表

给你链表的头结点 head &#xff0c;请将其按 升序 排列并返回 排序后的链表 。 示例 1&#xff1a; 输入&#xff1a;head [4,2,1,3] 输出&#xff1a;[1,2,3,4]示例 2&#xff1a; 输入&#xff1a;head [-1,5,3,4,0] 输出&#xff1a;[-1,0,3,4,5]示例 3&#xff1a; 输入…

人工智能OCR领域安全应用措施

引言 编写目的 随着新一轮科技革命和产业变革的深入发展&#xff0c;5G、大数据、云计算、深度学习等新技术日益成为推动社会进步的核心动力。人工智能&#xff08;AI&#xff09;作为这些新技术的集大成者&#xff0c;正迅速成为新型基础设施建设的战略性支柱&#xff0c;其广…

Spring Boot整合MyBatis Plus配置多数据源

Spring Boot 专栏&#xff1a;https://blog.csdn.net/dkbnull/category_9278145.html Spring Cloud 专栏&#xff1a;https://blog.csdn.net/dkbnull/category_9287932.html GitHub&#xff1a;https://github.com/dkbnull/SpringBootDemo Gitee&#xff1a;https://gitee.com/…

数字化转型导师坚鹏:科技金融政策、案例及营销创新

科技金融政策、案例及营销创新 课程背景&#xff1a; 很多银行存在以下问题&#xff1a; 不清楚科技金融有哪些利好的政策&#xff1f; 不知道科技金融有哪些成功的案例&#xff1f; 不知道科技金融如何进行营销创新&#xff1f; 课程特色&#xff1a; 以案例的方式解…

Tomcat容器经常重启问题排查

报错代码: INFO [Catalina-utility-2] org.apache.catalina.core.StandardContext.reload Reloading Context with name [] has started1.查看内存占用情况:top 可以发现java线程正常情况下占用高达24%的内存资源 2.继续排查:top -Hp 29580 可以发现主要有子线程Catalina-ut…

基于jsp+mysql+Spring+mybatis的SSM汽车保险理赔管理系统设计和实现

基于jspmysqlSpringmybatis的SSM汽车保险理赔管理系统设计和实现 博主介绍&#xff1a;多年java开发经验&#xff0c;专注Java开发、定制、远程、文档编写指导等,csdn特邀作者、专注于Java技术领域 作者主页 央顺技术团队 Java毕设项目精品实战案例《1000套》 欢迎点赞 收藏 ⭐…

Node-RED在Linux二次开发网关中能源数据实时采集与优化

智能电网与分布式能源系统已成为推动绿色能源转型的重要载体。为了更好地应对多样化的能源供给与需求挑战&#xff0c;以及实现更高效的能源管理&#xff0c;Linux二次开发网关与Node-RED这一创新组合应运而生。 Linux二次开发网关作为高度定制化的硬件平台&#xff0c;其开源特…

MT笔试题

前言 某团硬件工程师的笔试题&#xff0c;个人感觉题目的价值还是很高的&#xff0c;分为选择题和编程题&#xff0c;选择题考的是嵌入式基础知识&#xff0c;编程题是两道算法题&#xff0c;一道为简单难度&#xff0c;一道为中等难度 目录 前言选择题编程题 选择题 C语言中变…

【MATLAB】语音信号识别与处理:一维信号NLM非局部均值滤波算法去噪及谱相减算法呈现频谱

1 基本定义 一维信号NLM非局部均值滤波算法是一种基于非局部均值思想的滤波方法&#xff0c;它通过对信号进行分块&#xff0c;计算每个块与其他块之间的相似度&#xff0c;以非局部均值的方式去除噪声。该算法的主要思想是在一定范围内寻找与当前块相似的块&#xff0c;以这些…

基于网络爬虫的购物平台价格监测系统的设计与实现

通过对网络爬虫的购物平台价格监测系统的业务流程进行梳理可知&#xff0c;网络爬虫的购物平台价格监测系统主要由前台买家模块、后台卖家模块以及管理员模块构成。前台功能包含登录功能、注册功能、系统首页功能、唯品会商品详情浏览、唯品会商品收藏、唯品会商品点赞、唯品会…

RDD算子介绍(二)

1. coalesce 用于缩减分区&#xff0c;减少分区个数&#xff0c;减少任务调度成本。 val rdd : RDD[Int] sc.makeRDD(List(1, 2, 3, 4), 4) val newRDD rdd.coalesce(2) newRDD.saveAsTextFile("output") 分区数可以减少&#xff0c;但是减少后的分区里的数据分布…

政安晨:【深度学习处理实践】(五)—— 初识RNN-循环神经网络

RNN&#xff08;循环神经网络&#xff09;是一种在深度学习中常用的神经网络结构&#xff0c;用于处理序列数据。与传统的前馈神经网络不同&#xff0c;RNN通过引入循环连接在网络中保留了历史信息。 RNN中的每个神经元都有一个隐藏状态&#xff0c;它会根据当前输入和前一个时…

SRS(Simple Realtime Server)

SRS(Simple Realtime Server - github) SRS 中文官网 docker安装srs ##&#xff08;安全组放开1935端口、8080端口&#xff09; docker run --rm -it -p 1935:1935 -p 1985:1985 -p 8080:8080 -p 8000:8000/udp -p 10080:10080/udp ossrs/srs:5推流 ## 不需要加端口 ffmpeg…

深度学习armv8/armv9 cache的原理

文章目录 1、为什么要用cache?2、背景:架构的变化?2、cache的层级关系 ––big.LITTLE架构&#xff08;A53为例)3、cache的层级关系 –-- DynamIQ架构&#xff08;A76为例)4、DSU / L3 cache5、L1/L2/L3 cache都是多大呢6、cache相关的术语介绍7、cache的分配策略(alocation,…
最新文章