【发布时间】:2018-09-07 01:17:30
【问题描述】:
我找到了在我的 c++ 应用程序中使用的回调计时器的以下实现。然而,这个实现需要我从start调用者“加入”线程,这有效地阻塞了start函数的调用者。
我真正喜欢做的是以下。
- 有人可以多次调用 foo(data) 并将它们存储在数据库中。
- 每当调用 foo(data) 时,它都会启动一个计时器几秒钟。
- 当计时器倒计时,foo(data) 可以被多次调用 可以存储时间和多个项目,但在计时器完成之前不会调用擦除
- 每当计时器到时, "remove" 函数被调用一次以从 分贝。
基本上我希望能够执行一项任务,然后等待几秒钟,然后在几秒钟后批量执行单个批处理任务 B。
class CallBackTimer {
public:
/**
* Constructor of the CallBackTimer
*/
CallBackTimer() :_execute(false) { }
/**
* Destructor
*/
~CallBackTimer() {
if (_execute.load(std::memory_order_acquire)) {
stop();
};
}
/**
* Stops the timer
*/
void stop() {
_execute.store(false, std::memory_order_release);
if (_thd.joinable()) {
_thd.join();
}
}
/**
* Start the timer function
* @param interval Repeating duration in milliseconds, 0 indicates the @func will run only once
* @param delay Time in milliseconds to wait before the first callback
* @param func Callback function
*/
void start(int interval, int delay, std::function<void(void)> func) {
if(_execute.load(std::memory_order_acquire)) {
stop();
};
_execute.store(true, std::memory_order_release);
_thd = std::thread([this, interval, delay, func]() {
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
if (interval == 0) {
func();
stop();
} else {
while (_execute.load(std::memory_order_acquire)) {
func();
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
}
}
});
}
/**
* Check if the timer is currently running
* @return bool, true if timer is running, false otherwise.
*/
bool is_running() const noexcept {
return ( _execute.load(std::memory_order_acquire) && _thd.joinable() );
}
private:
std::atomic<bool> _execute;
std::thread _thd;
};
我已经尝试使用thread.detach() 修改上面的代码。但是,我在无法从数据库中写入(擦除)的分离线程中运行问题..
感谢任何帮助和建议!
【问题讨论】:
-
你能添加一个你是如何使用这个类的示例吗?您所要求的似乎有风险且不必要。
-
也许你可以在构造函数中构造线程并在析构函数中加入它?然后您只需要一个 std::queue 即可通过 start() 添加新项目(回调)并通过 stop() 刷新所有元素。一旦超出范围,析构函数将处理线程。然后线程中会有一个while循环,直到队列有任何项目。然后开始处理它。下一个 while 循环将检查计时器和项目数。在内部,它会一个接一个地处理项目。然后让它重复直到停止。析构函数将进行清理。我猜项目之间的间隔是等待,延迟只是它的触发时间。
标签: c++ multithreading timer pthreads