Проблема с использованием std :: atomic_flag с рабочим потоком
извините за многословие - я приложил все усилия, чтобы сжать мой пример кода в минимально функциональный класс и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