C++可运行类
我想实现类似于 Java 接口 Runnable 的东西。我尝试这样做:
class Runnable{
public:
void start(){
t = std::thread(&Runnable::run, this);
}
protected:
virtual void run(){
}
};
想法很简单。我想重载 run 方法,然后 start() 应该启动重载的方法。但是……这不起作用。
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
PS 我使用 dlopen 从动态库加载派生自 Runnable 的类实例。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当 GCC 忘记在命令行中使用
-pthread
时,通常会出现此错误。This error is commonly seen produced by GCC when forgetting to use
-pthread
at command line.我发现您的代码存在一个问题:不允许您从类的构造函数中调用虚函数。这样做会产生未定义的行为。
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.