在工作线程中使用std :: atomic_flag的问题 [英] Issue using std::atomic_flag with worker thread
问题描述
很抱歉-我已尽我所能将代码示例压缩为功能最少的类和main()
方法.
sorry for the verbosity - I did my best to condense my code sample into a minimally functional class and main()
method.
我正在尝试使用atomic_flag
通知工作线程中的_rx()
在调用stop()
时退出.
I'm trying to use an atomic_flag
to notify _rx()
within my worker thread to quit when stop()
is called.
我认为问题在于尝试创建我的工作线程,
I believe the issue is in trying to create my worker thread,
thread SanityTestThread(&SanityTest::_rx, *this);
与我的atomic_flag
代码示例(无法编译):
code sample (does not compile):
#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循环来编译和运行程序,如下所示:
For the record, I can get my program to compile and run by removing all references to my atomic_flag
and replacing my _rx()
loop with a for loop like so:
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:
p.s.这是用g++ -pthread -std=c++0x -o SanityTest ./SanityTest.cpp
推荐答案
只需替换
thread SanityTestThread(&SanityTest::_rx, *this);
与
thread SanityTestThread(&SanityTest::_rx, this);
您可能打算将指针传递给对象,而不是对象本身(这将导致复制该对象,并在该副本而不是原始对象上调用成员函数指针&SanityTest::_rx
).
You probably intended to pass a pointer to the object and not the object itself (which would result in that object being copied and the member function pointer &SanityTest::_rx
being invoked on that copy instead of the original object).
产生此错误消息的原因实质上是std::atomic_flag
没有副本构造函数,因此编译器也不会为SanityTest
类生成默认的构造函数,但是再次:您不想无论如何,请在此处复制您的SanityTest对象.
The reason for the error message is essentially that std::atomic_flag
doesn't have a copy constructor and so the compiler doesn't generate a default one for your SanityTest
class either, but again: you don't want to copy your SanityTest object here anyway.
这篇关于在工作线程中使用std :: atomic_flag的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!