Ai
1 Star 1 Fork 0

钦某/Code

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
thread.hpp 1.12 KB
一键复制 编辑 原始数据 按行查看 历史
钦某 提交于 2025-04-17 22:35 +08:00 . 基于Udp聊天室
#include <iostream>
#include <string>
#include <unistd.h>
#include <pthread.h>
#include <functional>
using func_t = std::function<void()>;
class Thread
{
private:
static void *routine(void *args)
{
Thread *self = static_cast<Thread *>(args);
if (self->_is_detach)
pthread_detach(self->_tid);
self->_func();
return nullptr;
}
public:
Thread(func_t func)
: _tid(0),
_func(func),
_is_detach(false)
{
}
bool Start()
{
if (_tid == 0)
{
pthread_create(&_tid, nullptr, routine, this);
return true;
}
return false;
}
void Detach()
{
if (_is_detach)
{
return;
}
else if (_tid != 0)
{
pthread_detach(_tid);
}
_is_detach = true;
}
bool Join()
{
if (_tid != 0)
{
pthread_join(_tid, nullptr);
return true;
}
return false;
}
~Thread()
{
}
private:
pthread_t _tid;
func_t _func;
bool _is_detach;
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/wang-qin928/cpp.git
git@gitee.com:wang-qin928/cpp.git
wang-qin928
cpp
Code
master

搜索帮助