[英]Passing an atomic variable to a function
我试图将原子变量传递给函数如下:
// function factor receives an atomic variable
void factor(std::atomic<int> ThreadsCounter)
{
.........
}
// main starts here
int main()
{
// Atomic variable declaration
std::atomic<int> ThreadsCounter(0);
// passing atomic variable to the function factor through a thread
Threadlist[0] = std::thread (factor, std::ref(ThreadsCounter));
Threadlist[0].join();
return 0;
}
运行上面的代码时,我收到以下错误:
错误2错误C2280:'std :: atomic :: atomic(const std :: atomic&)':尝试引用已删除的函数c:\\ program files(x86)\\ microsoft visual studio 12.0 \\ vc \\ include \\ functional 1149 1 klu_factor
谁知道如何解决这个问题? 非常感谢您的帮助。
函数factor
按值获取ThreadsCounter
参数, std::atomic
不是可复制构造的。
虽然您绑定了对线程函数的引用,但它正在尝试创建一个副本来传递该函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.