Ai
1 Star 0 Fork 0

Shawy/2023Linux

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Thread.hpp 980 Bytes
一键复制 编辑 原始数据 按行查看 历史
Shawy 提交于 2023-04-26 15:17 +08:00 . ThreadPool Test1
#pragma once
#include <iostream>
#include <pthread.h>
#include <string>
#include <functional>
// typedef std::function<void* (void*)> fun_t;
typedef void* (*fun_t)(void*); // 定义一个函数指针类型
// (要传递给)线程的信息
class ThreadData
{
public:
void* _args; // 线程参数
std::string _name; // 线程名称
};
// 线程类
class Thread
{
public:
Thread(int num, fun_t callback, void* args)
: _fun(callback)
{
char threadName[64];
snprintf(threadName, sizeof(threadName), "Thread:[%d]", num);
_name = threadName;
_td._args = args; // 给线程传递参数
_td._name = _name;
}
~Thread()
{}
// 创建线程
void start()
{
pthread_create(&_tid, nullptr, _fun, (void*)&_td);
}
void join()
{
pthread_join(_tid, nullptr);
}
std::string name()
{
return _name;
}
private:
ThreadData _td; // 要传递给线程的信息
std::string _name; // 线程名称
pthread_t _tid; // 线程ID
fun_t _fun; // 线程函数地址
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/shawyxy/2023-linux.git
git@gitee.com:shawyxy/2023-linux.git
shawyxy
2023-linux
2023Linux
main

搜索帮助