我有这样的基类:纯虚函数派生类
class Base {
public:
Base();
virtual ~Base();
protected:
virtual on_next_item(std::string& item) = 0;
private:
void read_loop();
};
和此派生类:
class Derived : public Base {
public:
Derived();
virtual ~Derived();
protected:
void on_next_item(std::string& item) override;
};
在Base
类构造我开始一个螺纹,其从套接字读取并调用在派生类上调用的on_next_item()
。 在Base
析构函数中,reader线程通过原子标志停止。 但有时read_loop
仍然叫on_next_item
,我得到一个“纯粹的虚函数调用!”错误。我假设我在竞赛状态下运行:
子类(对象)已经被破坏,因此函数不再被注册。
有没有合适的方法来解决这种竞争条件?
为了完整这里的读者循环:在Base
类的析构
while (running.load())
{
string item = read();
if (running.load())
{
on_text_item(item);
}
}
的行驶标志切换为false。
编辑(完整运行的例子,有被执行多次在这个问题上运行):
#include <atomic>
#include <boost/thread/thread.hpp>
#include <boost/chrono.hpp>
#include <iostream>
class Base
{
public:
Base() : running(true)
{
readerThread = new boost::thread(&Base::read_loop, this);
}
virtual ~Base()
{
running = false;
delete readerThread;
readerThread = nullptr;
}
protected:
virtual void on_next_item(std::string &item) = 0;
private:
boost::thread *readerThread;
void read_loop()
{
std::string element = "Element";
while (running.load())
{
boost::this_thread::sleep_for(boost::chrono::milliseconds(2));
on_next_item(element);
}
}
std::atomic_bool running;
};
class Derived : public Base
{
public:
Derived() : Base()
{
}
virtual ~Derived()
{
}
protected:
virtual void on_next_item(std::string &item)
{
std::cout << "On Next Item " << item << std::endl;
}
};
void constAndDestruct()
{
Derived d;
boost::this_thread::sleep_for(boost::chrono::seconds(2));
}
int main(int argc, char **argv)
{
constAndDestruct();
boost::this_thread::sleep_for(boost::chrono::seconds(2));
}
谢谢!
您能照常提供[MCVE]吗?其他任何事情都会导致纯粹的猜测。 – user0042
您可以将逻辑从'Base'的析构函数移动到成员函数。或者这不适合你? –
在构造函数中创建线程似乎不是一个好主意。同样在Base类构造函数中,对象没有完全形成(派生类尚未存在)。 https://stackoverflow.com/questions/30258639/when-is-it-safe-to-call-this-in-constructor-and-destructor – mksteve