将模板参数传递给 std::thread
passing template args to std::thread
我在将参数传递给调用模板函数的 std::thread 时遇到了一点问题。
template <typename F, typename... A>
void start(int interval, F func, A&&... args)
{
if (_running.load(std::memory_order_acquire)) {
stop();
};
_running.store(true, std::memory_order_release);
_thread = std::thread([this, interval, func, args]()
{
while (_running.load(std::memory_order_acquire))
{
func(std::forward<A>(args)...);
for (int i = 0; (i <= (interval / 200)) && _running.load(std::memory_order_acquire); i++)
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
});
}
编辑:添加了更多细节我应该能够传递任何函数以使用任何 ammount 或参数"启动"
void ThreadProc(HWND hwnd)
{
Beep(1000, 100);
}
start(2000, ThreadProc, hwnd);
错误:
error C3520: 'args': parameter pack must be expanded in this context
note: see reference to function template instantiation 'void CallBack::start<void(__cdecl *)(HWND),HWND&>(int,F,HWND &)' being compiled
1> with
1> [
1> F=void (__cdecl *)(HWND)
1> ]
_thread = std::thread([this, interval, func, &args...]()
相关文章:
- 在std::thread中,joinable()然后join()线程安全吗
- 分离一个静态常量 std::thread?
- 当指向对象的指针作为参数传递给 std::thread 时,内存可见性
- 如何从 std::thread 返回值
- 将 std::thread by 值推送到列表中
- 转发变量参数列表以模拟 std::thread
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- std::thread 增加 DLL 引用计数,从而防止卸载 DLL
- 如何防止 std::thread 在 QT 中冻结 GUI?
- 对带有唯一指针的 std::thread 使用类成员函数时出现编译错误
- 为什么参数在构造 std::thread 时移动两次
- std::thread::_Invoker 使用线程编程时出错
- 在线程 A 中创建一个 std::thread 对象,在线程 B 中连接
- 为什么编译器抱怨 std::thread 参数在转换为右值后必须是可调用的?
- ZeroMQ 在使用 std::thread 创建工作线程时崩溃
- 在没有复制构造函数的对象的成员函数中启动 std::thread
- CLang:在 std::thread 中运行函数会导致结构创建BAD_ACCESS
- 调用以CWinThread为基的类运算符()的std::thread失败
- 如何通过std::thread生成多个线程
- std::future::get()或std::future::wait()是std::thread::join()的替