C++ 对工作线程使用std::atomic_标志时出现问题
很抱歉这么冗长-我尽了最大努力将代码示例压缩为一个最小的函数类和C++ 对工作线程使用std::atomic_标志时出现问题,c++,multithreading,c++11,stdatomic,C++,Multithreading,C++11,Stdatomic,很抱歉这么冗长-我尽了最大努力将代码示例压缩为一个最小的函数类和main()方法 我正试图使用原子标记在调用stop()时通知工作线程中的\u rx()退出 我相信问题在于如何创建我的工作线程 thread SanityTestThread(&SanityTest::_rx,*this) 这与我的原子标志 代码示例(不编译): 编译器错误: In file included from ./SanityTest.cpp:1:0: ./SanityTest.hpp:14:7: note: ‘Sani
main()
方法
我正试图使用原子标记
在调用stop()
时通知工作线程中的\u rx()
退出
我相信问题在于如何创建我的工作线程
thread SanityTestThread(&SanityTest::_rx,*this)代码>
这与我的原子标志
代码示例(不编译):
编译器错误:
In file included from ./SanityTest.cpp:1:0:
./SanityTest.hpp:14:7: note: ‘SanityTest::SanityTest(SanityTest&&)’ is implicitly deleted because the default definition would be ill-formed:
class SanityTest
^
./SanityTest.hpp:14:7: error: use of deleted function ‘std::atomic_flag::atomic_flag(const std::atomic_flag&)’
In file included from /usr/include/c++/4.8/atomic:41:0,
from ./SanityTest.hpp:8,
from ./SanityTest.cpp:1:
/usr/include/c++/4.8/bits/atomic_base.h:275:5: error: declared here
atomic_flag(const atomic_flag&) = delete;
^
In file included from /usr/include/c++/4.8/functional:55:0,
from /usr/include/c++/4.8/thread:39,
from ./SanityTest.hpp:10,
from ./SanityTest.cpp:1:
...
In file included from ./SanityTest.cpp:1:0:
./SanityTest.hpp:14:7: note: ‘SanityTest::SanityTest(SanityTest&&)’ is implicitly deleted because the default definition would be ill-formed:
class SanityTest
^
./SanityTest.hpp:14:7: error: use of deleted function ‘std::atomic_flag::atomic_flag(const std::atomic_flag&)’
In file included from /usr/include/c++/4.8/atomic:41:0,
from ./SanityTest.hpp:8,
from ./SanityTest.cpp:1:
/usr/include/c++/4.8/bits/atomic_base.h:275:5: error: declared here
atomic_flag(const atomic_flag&) = delete;
^
In file included from /usr/include/c++/4.8/functional:55:0,
from /usr/include/c++/4.8/thread:39,
from ./SanityTest.hpp:10,
from ./SanityTest.cpp:1:
p、 这是用,g++-pthread-std=c++0x-o SanityTest./SanityTest.cpp编译的,只需替换
thread SanityTestThread(&SanityTest::_rx, *this);
与
您可能希望传递指向对象的指针,而不是对象本身(这将导致复制该对象,并在该副本而不是原始对象上调用成员函数指针&SanityTest::_rx
)
产生错误消息的原因主要是std::atomic_flag
没有复制构造函数,因此编译器也不会为您的SanityTest
类生成默认构造函数,但是,无论如何,你也不想复制你的SunItTestRestor。也可以通过C++来引用参数。C++11帮了我很大的忙,但它也让我觉得自己对这个游戏还是一个陌生人。长话短说,感谢您的轻松修复。请参阅类似问题的答案。谢谢@frymode!我对自己的问题没有足够的了解,因此不知道首先要搜索什么。请查看,我将此内容转化为一篇博客文章:
In file included from ./SanityTest.cpp:1:0:
./SanityTest.hpp:14:7: note: ‘SanityTest::SanityTest(SanityTest&&)’ is implicitly deleted because the default definition would be ill-formed:
class SanityTest
^
./SanityTest.hpp:14:7: error: use of deleted function ‘std::atomic_flag::atomic_flag(const std::atomic_flag&)’
In file included from /usr/include/c++/4.8/atomic:41:0,
from ./SanityTest.hpp:8,
from ./SanityTest.cpp:1:
/usr/include/c++/4.8/bits/atomic_base.h:275:5: error: declared here
atomic_flag(const atomic_flag&) = delete;
^
In file included from /usr/include/c++/4.8/functional:55:0,
from /usr/include/c++/4.8/thread:39,
from ./SanityTest.hpp:10,
from ./SanityTest.cpp:1:
...
In file included from ./SanityTest.cpp:1:0:
./SanityTest.hpp:14:7: note: ‘SanityTest::SanityTest(SanityTest&&)’ is implicitly deleted because the default definition would be ill-formed:
class SanityTest
^
./SanityTest.hpp:14:7: error: use of deleted function ‘std::atomic_flag::atomic_flag(const std::atomic_flag&)’
In file included from /usr/include/c++/4.8/atomic:41:0,
from ./SanityTest.hpp:8,
from ./SanityTest.cpp:1:
/usr/include/c++/4.8/bits/atomic_base.h:275:5: error: declared here
atomic_flag(const atomic_flag&) = delete;
^
In file included from /usr/include/c++/4.8/functional:55:0,
from /usr/include/c++/4.8/thread:39,
from ./SanityTest.hpp:10,
from ./SanityTest.cpp:1:
thread SanityTestThread(&SanityTest::_rx, *this);
thread SanityTestThread(&SanityTest::_rx, this);