I am trying to create a service object that can be started (i.e. execute its run() ) in a separate thread. This is a 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 create a test class that inherits from this abstract class and is called in the main() function
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 that I am doing this, why am I getting a pure virtual function called error message? The run() function is explicitly overridden in the test class. What's worse, does it work 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 wrong here?
c ++ inheritance multithreading virtual-functions
subzero
source share