🌏博客主页:PH_modest的博客主页
🚩当前专栏:Linux跬步积累
💌其他专栏:
🔴 每日一题
🟡 C++跬步积累
🟢 C语言跬步积累
🌈座右铭:广积粮,缓称王!
创建文件
首先创建一个.hpp文件和一个.cc文件,.hpp文件内进行对thread的封装,然后在.cc文件下进行测试。
提示:.hpp文件是类似于.h文件,更多用于C++中,用于和C语言的进行区分,.hpp文件内包含类、模版、函数、宏的定义和声明,它允许在多个源文件中共享代码,避免重复编写。
.hpp文件的创建、thread类的函数定义
#ifndef __THREAD_HPP__ //避免头文件重复包含
#define __THREAD_HPP__
#include<iostream>
#include<string>
#include<pthread.h>
#include<functional>
#include<unistd.h>
//命名空间
namespace ThreadModule
{
template<class T>
using func_t = std::function<void(T&)>;//模版方法,下面参数的定义也要带模版
template<class T>
class Thread
{
public:
Thread(func_t<T> func,const T &data,const std::string &name="none_name")
:_func(func),_data(data),_threadname(name),_stop(true)
{
}
bool Start()//启动并创建线程
{
}
void Detach()//分离线程
{