开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
forked from Long_xu/cppThreadPool
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
main
分支 (1)
main
main
分支 (1)
main
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
main
分支 (1)
main
cppThreadPool
/
src
/
cppThreadPool.cpp
cppThreadPool
/
src
/
cppThreadPool.cpp
cppThreadPool.cpp 3.53 KB
一键复制 编辑 原始数据 按行查看 历史
Long_xu 提交于 2023年01月29日 17:23 +08:00 . src
#include "cppThreadPool.h"
CPP_ThreadPool::CPP_ThreadPool()
:_threadNum(1),_bTerminate(false)//构造函数,初始线程数为1
{
}
// 析构函数,结束线程池
CPP_ThreadPool::~CPP_ThreadPool()
{
stop();
}
bool CPP_ThreadPool::init(size_t num)
{
unique_lock<mutex> lock(_mutex);
if(!_threads.empty())
return false;
_threadNum=num;
return true;
}
void CPP_ThreadPool::stop()
{
// 注意要有这个{},不然会死锁。
{
unique_lock<mutex> lock(_mutex);
_bTerminate=true;
_condition.notify_all();
}
size_t thdCount=_threads.size();
for(size_t i=0;i<thdCount;i++)
{
if(_threads[i]->joinable())
{
_threads[i]->join();
}
delete _threads[i];
_threads[i]=NULL;
}
unique_lock<mutex> lock(_mutex);
_threads.clear();
}
bool CPP_ThreadPool::start()
{
unique_lock<mutex> lock(_mutex);
if(!_threads.empty())
return false;
for(size_t i=0;i<_threadNum;i++)
{
_threads.push_back(new thread(&CPP_ThreadPool::run,this));
}
return true;
}
bool CPP_ThreadPool::get(TaskFuncPtr& task)
{
unique_lock<mutex> lock(_mutex);
if(_tasks.empty())
{
_condition.wait(lock,[this]{
return _bTerminate || !_tasks.empty();
});
}
if(_bTerminate)
return false;
if(!_tasks.empty())
{
task=move(_tasks.front());
_tasks.pop();
return true;
}
return false;
}
// 执行任务的线程
void CPP_ThreadPool::run()
{
while(!isTerminate())
{
TaskFuncPtr task;
// 读取任务
bool ok=get(task);
if(ok)
{
++_atomic;
try
{
if(task->_expireTime!=0 && task->_expireTime < TNOWMS)
{
// 处理超时任务
}
else
task->_func();//执行任务
}
catch(...)
{}
--_atomic;
// 任务执行完毕,这里只是为了通知waitForAllDone
unique_lock<mutex> lock(_mutex);
if(_atomic==0 && _tasks.empty())
_condition.notify_all();
}
}
}
bool CPP_ThreadPool::waitForAllDone(int millsecond)
{
unique_lock<mutex> lock(_mutex);
if(_tasks.empty())
return true;
if(millsecond<0)
{
_condition.wait(lock,[this]{ return _tasks.empty();});
return true;
}
else
{
return _condition.wait_for(lock,chrono::milliseconds(millsecond),[this]{ return _tasks.empty();});
}
}
int gettimeofday(struct timeval &tv)
{
#if WIN32
time_t clock;
struct tm tm;
SYSTEMTIME wtm;
GetLocalTime(&wtm);
tm.tm_year = wtm.wYear - 1900;
tm.tm_mon = wtm.wMonth - 1;
tm.tm_mday = wtm.wDay;
m.tm_hour = wtm.wHour;
tm.tm_min = wtm.wMinute;
tm.tm_sec = wtm.wSecond;
tm. tm_isdst = -1;
clock = mktime(&tm);
tv.tv_sec = clock;
tv.tv_usec = wtm.wMilliseconds * 1000;
return 0;
#else
return ::gettimeofday(&tv, 0);
#endif
}
void getNow(timeval *tv)
{
#if TARGET_PLATFORM_IOS || TARGET_PLATFORM_LINUX
int idx = _buf_idx;
*tv = _t[idx];
if(fabs(_cpu_cycle - 0) < 0.0001 && _use_tsc)
{
addTimeOffset(*tv, idx);
}
else
{
TC_Common::gettimeofday(*tv);
}
#else
gettimeofday(*tv);
#endif
}
int64_t getNowMs()
{
struct timeval tv;
getNow(&tv);
return tv.tv_sec * (int64_t)1000 + tv.tv_usec / 1000;
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

C++实现的线程池,涉及以下知识点: decltype。 packaged_task。 make_shared。 mutex。 unique_lock。 notify_one。 future。 queue。 bind。 thread。 等等。
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/outspace/cppThreadPool.git
git@gitee.com:outspace/cppThreadPool.git
outspace
cppThreadPool
cppThreadPool
main
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /