【发布时间】:2018-04-20 09:47:14
【问题描述】:
我不确定这是否是 c++11 中的预期行为。这是我发现的一个示例。
#include <iostream>
#include <thread>
using namespace std;
class A {
public:
virtual void a() = 0;
thread t;
A() : t(&A::a, this) {}
virtual ~A() {
t.join();
}
};
class B : public A {
public:
virtual void a() {
cout << "B::a" << endl;
}
};
int main() {
B b;
this_thread::sleep_for(chrono::seconds(1));
}
编译运行时
$ g++ -std=c++11 -pthread test.cpp -o test
$ ./test
B::a
$
但是当睡眠被移除时...
int main() {
B b;
//this_thread::sleep_for(chrono::seconds(1));
}
奇怪的事情发生了
$ g++ -std=c++11 -pthread test.cpp -o test
$ ./test
pure virtual method called
terminate called without an active exception
Aborted (core dumped)
$
这可能是一个错误吗?
【问题讨论】:
标签: c++ multithreading c++11