std::function带有定时器成员函数的c++
std::function With Member Function For Timer C++
我有一个定时器类,我已经设置了能够绑定到使用std::function
模板的自由浮动函数。我想修改类,以便能够支持使用自由浮动函数和类成员函数。我知道std::function
可以使用std::bind
绑定到成员函数,但我不确定如何使用我拥有的代码设置此:
#include <iostream>
#include <chrono>
#include <thread>
#include <functional>
#include <atomic>
namespace Engine {
template<class return_type,class...arguments>
class Timer{
typedef std::function<return_type(arguments...)> _function_t;
public:
Timer(size_t interval,bool autoRun,_function_t function,arguments...args){
_function = function;
_interval = interval;
if (autoRun) {
Enable(args...);
}
}
~Timer(){
if (Running()) {
Disable();
}
}
void Enable(arguments...args){
if (!Running()) {
_running=true;
enable(_interval, _function, args...);
}
}
void Disable(){
if (Running()) {
_running=false;
}
}
std::atomic_bool const& Running()const{
return _running;
}
protected:
void enable(size_t interval,_function_t func,arguments...args){
_thread =std::thread([&,func,interval,args...](){
std::chrono::duration<long long,std::nano> inter(interval);
auto __interval = std::chrono::microseconds(interval);
auto deadline = std::chrono::steady_clock::now();
while (_running) {
func(args...);
std::this_thread::sleep_until(deadline+=__interval);
}
});
_thread.detach();
}
protected:
_function_t _function;
std::atomic_bool _running;
size_t _interval;
std::thread _thread;
};
}
任何建议都很好。如果我需要澄清什么,请告诉我。
谢谢
要传递成员函数给this,传递一个指向未绑定成员函数(&Engine::SceneManager::Update
)的指针,然后第一个参数是指向应该调用该成员的对象的指针(指向SceneManager
对象的指针,这是"隐藏的"this
指针)。这就是bind的工作方式,因此不需要对代码进行任何更改。作为一种简单的替代方法,传递一个lambda。
http://coliru.stacked-crooked.com/a/7c6335d4f94b9f93(虽然它没有按预期运行,我不知道为什么)
另外,我对你的代码将internal作为size_t的事实感到困惑,然后将其转换为纳秒,然后将其转换为微秒,然后使用它。为什么不从头到尾都用微秒呢?
析构函数有一个竞争条件。Disable应该在线程完成执行之前停止。我没有使用std::thread
太多,但我想一个地方开始是if (_thread.is_joinable()) _thread.join();
作为其中的一部分,它可能是有用的,让线程一次只睡100ms左右,并定期检查它是否应该关闭。
Enable
应该在启动一个新线程之前停止现有线程。更好的做法是重用同一个线程。不幸的是,没有简单的方法可以让现有的线程切换任务,所以最简单的方法是简单地使用Disable
,然后保留现有的代码。
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- 如何获取std::result_of函数的返回类型
- 如何在c++中为模板函数实例创建快捷方式
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗