4

对冗长感到抱歉 - 我尽我所能将我的代码示例压缩成一个功能最少的类和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

作为记录,我可以通过删除对我的所有引用并用for 循环atomic_flag替换我的循环来编译和运行我的程序,如下所示:_rx()

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:

ps 这是编译的,g++ -pthread -std=c++0x -o SanityTest ./SanityTest.cpp

4

1 回答 1

2

只需更换

thread SanityTestThread(&SanityTest::_rx, *this);

thread SanityTestThread(&SanityTest::_rx, this);

您可能打算将指针传递给对象而不是对象本身(这将导致该对象被复制并且&SanityTest::_rx在该副本而不是原始对象上调用成员函数指针)。

错误消息的原因本质上std::atomic_flag是没有复制构造函数,因此编译器也不会为您的SanityTest类生成默认值,但同样:无论如何,您都不想在这里复制 SanityTest 对象。

于 2015-06-17T14:02:04.160 回答