gpt4 book ai didi

c++ - 将 std::atomic_flag 与工作线程一起使用时出现问题

转载 作者:太空狗 更新时间:2023-10-29 21:40:31 28 4
gpt4 key购买 nike

抱歉冗长 - 我已尽力将我的代码示例压缩为一个功能最少的类和 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 生成默认复制构造函数> class 要么,但是再一次:你不想在这里复制你的 SanityTest 对象。

关于c++ - 将 std::atomic_flag 与工作线程一起使用时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30893684/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com