我需要在X秒通过后重置一个类中的一些值.我正在使用这样的东西,但我不知道它可能有什么问题:
class Points
{
public:
Points();
~Points();
void reset();
...
private:
int number;
static void resetAfter5seconds(Points* points);
};
Run Code Online (Sandbox Code Playgroud)
在构造函数内部,我初始化一个分离的线程:
Points::Points(){
thread start(resetAfter5seconds, this);
start.detach();
}
void Points::resetAfter5seconds(Points* points){
while(true){
std::this_thread::sleep_for( (std::chrono::seconds(5)));
points->reset();
}
}
Run Code Online (Sandbox Code Playgroud)
这有效但我不知道它是否是正确的方法.它有任何记忆问题吗?有哪些替代方案?