简体   繁体   English

C ++继承:在重写时调用虚方法

[英]C++ Inheritance : Calling virtual method when it has been overridden

I am trying to build a service object which can run (ie execute it's run() function) in a separate thread. 我正在尝试构建一个service对象,它可以在一个单独的线程中运行(即执行它的run()函数)。 This is the service object 这是服务对象

#include <boost/noncopyable.hpp>
#include <atomic>
#include <thread>
#include <iostream>

class service : public boost::noncopyable {
 public:
  service() : stop_(false), started_(false) { }

  virtual ~service() {
    stop();
    if (thread_.joinable()) {
      thread_.join();
    }
  }

  virtual void stop() { stop_ = true; }

  virtual void start() {
    if (started_.load() == false) {
      started_ = true;
      thread_ = std::thread([&] () {
        run();
      });
    }
  }

 protected:
  virtual void run() = 0;

  std::atomic<bool> stop_;

  std::atomic<bool> started_;

  std::thread thread_;
};

I am the creating a test class which inherits from this abstract class and is called in the main() function 我正在创建一个继承自此抽象类的test类,并在main()函数中调用

class test : public service {
 public:
  test() : service() {
    std::cout<< "CTOR" << std::endl;
    start();
  }

  ~test() {
    std::cout<< "DTOR" << std::endl;
  }

 protected:
  void run() override {
    std::cout << "HELLO WORLD" <<std::endl;
  }
};


int main() {
  test test1;
  return 0;
}

Now when I execute this, why do I get an error saying pure virtual function called ? 现在,当我执行此操作时,为什么会出现一个错误,称pure virtual function called The run() function is clearly overridden in the test class. run()函数在test类中被明确覆盖。 Whats worse is that it runs correctly sometimes? 更糟糕的是它有时会正确运行吗?

$ ./a.out
CTOR
DTOR
pure virtual method called
terminate called without an active exception

$ ./a.out
CTOR
DTOR
pure virtual method called
terminate called without an active exception

$ ./a.out
CTOR
DTOR
pure virtual method called
terminate called without an active exception

$ ./a.out
CTOR
DTOR
HELLO WORLD

$ ./a.out
CTOR
DTOR
pure virtual method called
terminate called without an active exception

What could be going wrong here? 这可能会出错?

Follow along, step by step: 跟着,一步一步:

1) You construct the object. 1)构造对象。

2) You execute the following piece of code: 2)您执行以下代码:

if (started_.load() == false) {
  started_ = true;
  thread_ = std::thread([&] () {
    run();
  });
}

The parent thread immediately returns to main() where it immediately exits and destroys your object. 父线程立即返回main() ,它立即退出并销毁您的对象。

Here's your bug: 这是你的错误:

  • You are not guaranteed that the thread started in start() is going to reach the call to run() , above, before the parent thread terminates the process. 在父线程终止进程之前,无法保证在start()start()的线程将在上面调用run() Both the child thread, and the parent thread runs concurrently. 子线程和父线程同时运行。

So, every once in a while, the parent thread will destroy the object before the child thread gets in gear, and calls run (). 因此,每隔一段时间,父线程将在子线程开始之前销毁对象,并调用run ()。

At this point, the object whose run () method gets invoked, is already destroyed. 此时, run ()方法被调用的对象已被销毁。

Undefined behavior. 未定义的行为。

The assertion you're hitting, every once in a while, is one possible result of this undefined behavior. 你每次偶尔会遇到的断言是这种未定义行为的可能结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM