如何在 C++ 中以周期性间隔调用将对象作为参数的函数?
How to call a function at periodic interval that takes in an object as argument in C++?
我想定期执行一个以对象为参数的函数。
我尝试了这个答案: 以周期性间隔调用一个以整数作为参数的函数,但我无法弄清楚如何以周期性间隔调用以对象作为参数的函数.
#include <iostream>
#include <chrono>
#include <thread>
#include <functional>
#include <memory>
#include <atomic>
#include "window.h" // Contains the declaration of Window class
using cancel_token_t = std::atomic_bool;
template<typename Fnc>
void set_interval(Fnc fun, std::chrono::steady_clock::duration interval,
std::shared_ptr<cancel_token_t> cancel_token=nullptr)
{
std::thread([fun=std::move(fun), interval, tok=std::move(cancel_token)]()
{
while (!tok || !*tok) // Call until token becomes true (if it is set)
{
auto next = std::chrono::steady_clock::now() + interval;
fun();
std::this_thread::sleep_until(next);
}
}).detach();
}
void foo(Window &window)
{
// Do something with the window object
}
int main()
{
Window window;
using namespace std::chrono_literals;
auto cancel = std::make_shared<cancel_token_t>(false);
// Ordinary rules for lambda capture apply so be careful
// about lifetime if captured by reference.
set_interval([window]
{
foo(window);
}, 1000ms, cancel);
//set_interval([window]{foo(window);}, 1000ms); // Without token, runs until main exits.
std::this_thread::sleep_for(3s);
*cancel = true;
}
编译时出现如下错误:
'void foo(Window &)': cannot convert argument 1 from 'const Window' to 'Window &'
我怎样才能做到这一点?
谢谢
non-mutable lambda 的 operator()
是 const
。
当你通过复制捕获时,你不能改变你捕获的“成员”。
您可能想改为通过引用捕获:
set_interval([&window] { foo(window); }, 1000ms, cancel);
或者如果你真的想要复制,制作 lambda mutable
:
set_interval([window] mutable { foo(window); }, 1000ms, cancel);
我想定期执行一个以对象为参数的函数。
我尝试了这个答案:
#include <iostream>
#include <chrono>
#include <thread>
#include <functional>
#include <memory>
#include <atomic>
#include "window.h" // Contains the declaration of Window class
using cancel_token_t = std::atomic_bool;
template<typename Fnc>
void set_interval(Fnc fun, std::chrono::steady_clock::duration interval,
std::shared_ptr<cancel_token_t> cancel_token=nullptr)
{
std::thread([fun=std::move(fun), interval, tok=std::move(cancel_token)]()
{
while (!tok || !*tok) // Call until token becomes true (if it is set)
{
auto next = std::chrono::steady_clock::now() + interval;
fun();
std::this_thread::sleep_until(next);
}
}).detach();
}
void foo(Window &window)
{
// Do something with the window object
}
int main()
{
Window window;
using namespace std::chrono_literals;
auto cancel = std::make_shared<cancel_token_t>(false);
// Ordinary rules for lambda capture apply so be careful
// about lifetime if captured by reference.
set_interval([window]
{
foo(window);
}, 1000ms, cancel);
//set_interval([window]{foo(window);}, 1000ms); // Without token, runs until main exits.
std::this_thread::sleep_for(3s);
*cancel = true;
}
编译时出现如下错误:
'void foo(Window &)': cannot convert argument 1 from 'const Window' to 'Window &'
我怎样才能做到这一点?
谢谢
operator()
是 const
。
当你通过复制捕获时,你不能改变你捕获的“成员”。
您可能想改为通过引用捕获:
set_interval([&window] { foo(window); }, 1000ms, cancel);
或者如果你真的想要复制,制作 lambda mutable
:
set_interval([window] mutable { foo(window); }, 1000ms, cancel);