将原子变量传递给函数

Ana*_*nas 4 c++ atomic c++11

我试图将原子变量传递给函数如下:

 // 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;
 }
Run Code Online (Sandbox Code Playgroud)

运行上面的代码时,我收到以下错误:

错误2错误C2280:'std :: atomic :: atomic(const std :: atomic&)':尝试引用已删除的函数c:\ program files(x86)\ microsoft visual studio 12.0\vc\include\functional 1149 1 klu_factor

谁知道如何解决这个问题?非常感谢您的帮助.

Col*_*nee 6

该函数按值factor获取其ThreadsCounter参数,并且std::atomic不是可复制构造的.

虽然您绑定了对线程函数的引用,但它正在尝试创建一个副本来传递该函数.