Dejwi
Dejwi

Reputation: 4487

C++ Runnable class

I'd like to achieve something similar to interface Runnable from Java. I try to do that in that way:

class Runnable{
    public:
        void start(){
            t = std::thread(&Runnable::run, this);
        }
    protected:
        virtual void run(){
        }
};

Idea is simple. I'd like to overload run method and then start() should launch overloaded one. But... it doesn't work.

terminate called after throwing an instance of 'std::system_error'
  what():  Operation not permitted

PS I load an instance of class, which derives from Runnable, from dynamic library with dlopen.

Upvotes: 4

Views: 14645

Answers (2)

Cubbi
Cubbi

Reputation: 47448

This error is commonly seen produced by GCC when forgetting to use -pthread at command line.

Upvotes: 4

David Feurle
David Feurle

Reputation: 2787

I see one problem with your code: You are not allowed to call virtual functions from the constructor of the class. Doing so yields undefined behaviour.

Upvotes: 0

Related Questions