簡體   English   中英

使用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所有引用並用for循環替換我的_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

只需更換

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

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

您可能希望傳遞指向對象的指針而不是對象本身(這將導致該對象被復制,並且在該副本而不是原始對象上調用成員函數指針&SanityTest::_rx )。

錯誤消息的原因本質上是std::atomic_flag沒有復制構造函數,因此編譯器也不會為您的SanityTest類生成默認值,但是再次:您不想復制SanityTest對象無論如何這里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM