【Linux】:封装线程

devtools/2025/3/13 23:11:04/

朋友们、伙计们,我们又见面了,本期来给大家带来封装线程相关的知识点,如果看完之后对你有一定的启发,那么请留下你的三连,祝大家心想事成!

C 语 言 专 栏:C语言:从入门到精通

数据结构专栏:数据结构

个  人  主  页 :stackY、

C + + 专 栏   :C++

Linux 专 栏  :Linux

​ 

目录

引言:

1. 基础框架 

1.1 初步Start接口

1.2 修正后的Start接口

2. Join接口

 2.1 初步测试

3. 添加模版

4. 全部代码


引言:

我们想要通过封装原生线程库的方式来实现一个类似于C++11里面的线程库,这里只是为了来更沉入的学习原生线程库,实现一些基础的功能即可;

我们创建一个mian.cc文件用于测试线程逻辑;

Thread.hpp主要用于封装线程

Makefile主要实现自动化代码构建。

1. 基础框架 

  • 我们想要封装的线程需要有对应的线程id、线程名、该线程是否运行以及线程所要执行的任务;
  • 线程所需要执行的任务我们需要用函数包装器(functional)
  • 我们想要实现的方法有一个让线程启动起来的方法,还需要有一个等待线程的方法;
  • 后面根据需要添加对应的成员和方法。

Thread.hpp:

先将基础框架搭建出来,后面再慢慢补充所需要的接口以及代码。

1.1 初步Start接口

在Start接口就是用来启动线程,那么在接口设计中就需要先创建进程,在对应的线程执行方法中执行我们预先设计好的func;

这里就需要注意一下线程封装时的细节,我们先来看一下创建线程的接口具体传递的参数:

我们设置的线程执行方法函数的参数只有一个参数,但是因为我们自己设置的ThreadRoutine函数是类内函数,那么类内函数会默认自己带一个this指针,所以这于原始函数设计接口不符,所以我们需要将该函数设置为静态成员函数,然后我们将this指针传递给他,然后通过this指针来调用我们预设的func;

1.2 修正后的Start接口

2. Join接口

在实现Join之前我们可以设置一个查看线程是否运行的接口以及获取线程名的接口,方便后面的测试;

在等待线程这里我们直接使用原生线程库的接口:

我们目前不关心等待的结果;

 2.1 初步测试

上面的代码算是一份非常简单的线程封装代码,那么接下来我们使用main.cc来使用一下我们封装的线程库,因为我们将线程对象化了,所以我们就可以用容器来保存我们的线程,这其实就是一种“先描述,再组织”的过程。

#include <iostream>
#include <unistd.h>
#include <string>
#include <vector>#include "Thread.hpp"
// 设置线程名
std::string GetThreadName()
{static int num = 1;char buffer[64];snprintf(buffer, sizeof(buffer), "Thread-%d", num++);return buffer;
}void Print()
{while(1){std::cout << "hello thread" << std::endl;sleep(1);}
}int main()
{std::vector<Thread> threads;int num = 5;// 创建for(int i = 0; i< num; i++){threads.push_back(Thread(GetThreadName(),Print));}for (auto &t : threads){std::cout << t.ThreadName() << ", is running: " << t.IsRunning() << std::endl;}// 启动for (auto &t : threads){t.Start();}for (auto &t : threads){std::cout << t.ThreadName() << ", is running: " << t.IsRunning() << std::endl;}// Joinfor (auto &t : threads){t.Join();}return 0;
}

3. 添加模版

我们想给我们的线程传递参数,所以我们需要添加模版;

整体代码:

#pragma once
#include <iostream>
#include <string>
#include <functional>
#include <pthread.h>template <class T>
using func_t = std::function<void(T)>; // 任务template <class T>
class Thread
{
public:Thread(const std::string &threadname, func_t<T> func, T data): _tid(0), _thread_name(threadname), _isrunning(false), _func(func), _data(data){}// 执行方法static void* ThreadRoutine(void *args){Thread *ts = static_cast<Thread *>(args);ts->_func(ts->_data);return nullptr;}// 启动bool Start(){int n = pthread_create(&_tid, nullptr, ThreadRoutine, this); // 将this指针传递给ThreadRoutineif(n == 0){_isrunning = true;return true;}else return false;}// 等待bool Join(){if (!_isrunning)return false;int n = pthread_join(_tid, nullptr);if (n == 0){return true;}return false;}bool IsRunning(){return _isrunning;}std::string ThreadName(){return _thread_name;}~Thread() {}private:pthread_t _tid;           // 线程idstd::string _thread_name; // 线程名bool _isrunning;          // 线程是否运行func_t<T> _func;             // 线程所执行任务T _data;                   // 传递数据类型
};

4. 全部代码

Makefile

thread:main.ccg++ -o $@ $^ -std=c++11 -lpthread
.PHONY:clean
clean:rm -f thread

main.cc

#include <iostream>
#include <unistd.h>
#include <string>
#include <vector>#include "Thread.hpp"
// 设置线程名
std::string GetThreadName()
{static int num = 1;char buffer[64];snprintf(buffer, sizeof(buffer), "Thread-%d", num++);return buffer;
}void Print(int num)
{while(num--){std::cout << "hello thread num :" << num << std::endl;sleep(1);}
}int main()
{Thread<int> t(GetThreadName(), Print, 5);t.Start();t.Join();// std::vector<Thread> threads;// int num = 5;// // 创建// for(int i = 0; i< num; i++)// {//     threads.push_back(Thread(GetThreadName(),Print));// }// for (auto &t : threads)// {//     std::cout << t.ThreadName() << ", is running: " << t.IsRunning() << std::endl;// }// // 启动// for (auto &t : threads)// {//     t.Start();// }// for (auto &t : threads)// {//     std::cout << t.ThreadName() << ", is running: " << t.IsRunning() << std::endl;// }// // Join// for (auto &t : threads)// {//     t.Join();// }// Thread ts(Printf, GetThreadName());// std::cout << "is thread running? " << ts.IsRunning() << std::endl;// ts.Start();// std::cout << "is thread running? " << ts.IsRunning() << std::endl;// ts.Join();return 0;
}

Thread.hpp

#pragma once
#include <iostream>
#include <string>
#include <functional>
#include <pthread.h>template <class T>
using func_t = std::function<void(T)>; // 任务template <class T>
class Thread
{
public:Thread(const std::string &threadname, func_t<T> func, T data): _tid(0), _thread_name(threadname), _isrunning(false), _func(func), _data(data){}// 执行方法static void* ThreadRoutine(void *args){Thread *ts = static_cast<Thread *>(args);ts->_func(ts->_data);return nullptr;}// 启动bool Start(){int n = pthread_create(&_tid, nullptr, ThreadRoutine, this); // 将this指针传递给ThreadRoutineif(n == 0){_isrunning = true;return true;}else return false;}// 等待bool Join(){if (!_isrunning)return false;int n = pthread_join(_tid, nullptr);if (n == 0){return true;}return false;}bool IsRunning(){return _isrunning;}std::string ThreadName(){return _thread_name;}~Thread() {}private:pthread_t _tid;           // 线程idstd::string _thread_name; // 线程名bool _isrunning;          // 线程是否运行func_t<T> _func;             // 线程所执行任务T _data;                   // 传递数据类型
};


http://www.ppmy.cn/devtools/166879.html

相关文章

Qt配置OpenGL相机踩的坑

项目根据LearnOpenGL配置Qt的相机&#xff0c;更新view矩阵和project矩阵的位移向量变得很大&#xff0c;我设置的明明相机位置是(0,0,3)&#xff0c;理想的位移向量刚好是相反数(0,0,-3)&#xff0c;对应的view矩阵位置向量可以变成(0,0,1200)…离模型非常远矩阵模型也看不见&…

Prompt engineering设计原则(一)

目录 一、清晰具体的prompt1. 使用分隔符2. 结构化的输出&#xff08;JSON&#xff09;3. 要求模型检查是否满足条件4. 提供少量案例 二、给模型时间去思考1.指定完成任务所需的步骤2. 指导模型在下结论之前找出一个自己的解法 一、清晰具体的prompt 一个合理的prompt设计决定…

大语言模型学习--向量数据库Milvus实践

Milvus是目前比较流行的开源向量数据库&#xff0c;其官网地址 Milvus 是什么&#xff1f; | Milvus 文档 1.Milvus简介 Milvus 是一种高性能、高扩展性的向量数据库。Milvus 提供强大的数据建模功能&#xff0c;能够将非结构化或多模式数据组织成结构化的 Collections。它支…

一、Jenkins简单配置(使用语言、凭证、SSH)

这里简单讲一下jenkins的使用配置。 一、登陆系统 我们访问jenkins的界面的时候&#xff0c;被要求输入管理员密码&#xff0c;密码可以通过以下方式获取。 # 查看密码&#xff0c; 需要记住这个初始密码 # 在创建角色之后&#xff0c;这个保存密码的文件就会被删除 docker …

NGINX介绍--鱼皮老师课程学习笔记

世界上最受欢迎的web服务器、高性能负载均衡器、反向代理、API网关和内容缓存 Nginx能部署网站&#xff0c;比其他服务器用更少的资源&#xff0c;同时处理更多的用户请求&#xff0c;让网站速度更快更稳定 一、安装nginx windows双击exe启动 linux系统手动编译该目录 sudo …

CTFshow 【WEB入门】信息搜集 【VIP限免】 web1-web17

CTFshow 【 WEB入门】、【VIP限免】 web1 ----源码泄露 首先第一步&#xff0c;看源代码 web2----前台JS绕过 简单点击查看不了源代码&#xff0c;可以强制查看 比如 Ctrl Shift ICtrl U或者在url前加一个view-source: view-source:http://79999ca1-7403-46da-b25b-7ba9…

机器人匹诺曹机制,真话假话平衡机制

摘要&#xff1a; 本文聚焦于机器人所采用的一种“匹诺曹机制”&#xff0c;该机制旨在以大概率保持“虚拟鼻子”&#xff08;一种象征虚假程度的概念&#xff09;不会过长&#xff0c;通过在对话中夹杂真话与假话来实现。文章深入探讨了这一机制的原理&#xff0c;分析其背后的…

什么是zookeeper

ZooKeeper 是一个由 Apache 开源的分布式协调服务&#xff0c;专门为分布式系统提供高效、可靠的协同管理功能。它通过简单的接口和高效的设计&#xff0c;帮助开发者解决分布式环境中的常见问题&#xff0c;如配置管理、服务注册与发现、分布式锁、集群选举等。 --- ### 核心…