将 std::atomic_flag 与工作线程一起使用时出现问题
Issue using std::atomic_flag with worker thread
很抱歉冗长 - 我尽力将我的代码示例压缩为一个功能最小的类和main()
方法。
我正在尝试使用atomic_flag
来通知工作线程中的_rx()
在调用stop()
时退出。
我相信问题在于尝试创建我的工作线程,
thread SanityTestThread(&SanityTest::_rx, *this);
这不知何故与我的atomic_flag
冲突
代码示例(不编译):
#include <cstdio>
#include <chrono>
#include <unistd.h>
#include <atomic>
#include <iostream>
#include <thread>
using namespace std;
class SanityTest
{
public:
SanityTest(){}
void start();
void stop();
private:
void _rx();
atomic_flag flag;
}; // end class SanityTest
void SanityTest::_rx()
{
while(flag.test_and_set())
{
this_thread::sleep_for(chrono::seconds(1));
cout << "'sup foo" << endl;
}
} // end _rx
void SanityTest::start()
{
flag.test_and_set();
thread SanityTestThread(&SanityTest::_rx, *this);
SanityTestThread.detach();
} // end start
void SanityTest::stop()
{
flag.clear();
} // end start
int main(){
SanityTest s;// = SanityTest();
s.start();
this_thread::sleep_for(chrono::seconds(10));
s.stop();
return 0;
} // end main
作为记录,我可以通过删除对atomic_flag
的所有引用并将我的 _rx()
循环替换为 for 循环来编译和运行,如下所示:
void SanityTest::_rx()
{
for(int i=0; i <=10; ++ i)
{
this_thread::sleep_for(chrono::seconds(1));
cout << "'sup foo" << endl;
}
} // end _rx
编译器错误:
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:
附言这是编译的,g++ -pthread -std=c++0x -o SanityTest ./SanityTest.cpp
只需替换
thread SanityTestThread(&SanityTest::_rx, *this);
跟
thread SanityTestThread(&SanityTest::_rx, this);
您可能打算传递指向对象的指针,而不是对象本身(这将导致复制该对象,并且&SanityTest::_rx
在该副本而不是原始对象上调用成员函数指针)。
错误消息的原因本质上是std::atomic_flag
没有复制构造函数,因此编译器也不会为您的SanityTest
类生成默认构造函数,但同样:无论如何,您都不想在此处复制SanityTest对象。
相关文章:
- 如何将GTest与CMake一起使用?遵循谷歌指南时的链接问题
- 在浮点精度成为一个问题之前,可以将多少个浮点值加在一起
- 将cpp_redis pub/sub与条件变量一起使用时出现问题
- 在本地安装OpenMPI的问题与CUDA一起使用
- 与自定义命名空间一起使用时实现 std::error_category、名称解析问题
- 与lambda一起使用虚拟继承在初始化列表中捕获此问题的GCC错误
- 将 cplex 与 netbeans c++ 一起使用,链接器问题
- 将双缓冲位图与 AlphaBlend 一起使用时出现的问题
- 有关将智能指针与 Winsock 的 addrinfo 结构一起使用的问题
- QT TreeView问题与排序模型一起使用
- 将模板气泡排序与结构数组一起使用时出现问题
- 将 boost/thread headers 与 clang++ (Windows) 一起使用时出现问题
- 将 Botan 与 Qt 一起使用时遇到问题
- 与私人成员一起上课.这个代码出了什么问题
- 将指针与动态数组一起使用时出现问题
- 将问题与多个文件链接在一起
- Clang:将绑定或mem_fn与字符串::c_str和转换一起使用时出现问题
- 将get_value与 Boost 的属性树一起使用时遇到问题
- 将字符串与 sprintf() 函数一起使用时出现问题
- 将矢量与函数一起使用,指针问题