给定这段代码,那会是未定义的行为吗?
struct S {
void foo() {
// non-static member function, but not using "this"
}
};
int main() {
S* s = new S;
std::thread t(std::bind(&S::foo, s));
delete s;
}
这里的要点是:“this”在 foo() 中根本没有使用,但它是一个普通的成员函数。尽管另一个线程仍在运行,我是否可以销毁该实例?
(我知道出于所有实际目的,这不会崩溃。但它是未定义的行为吗?)
第二:如果 foo() 使用成员变量怎么办,但我保证(通过锁定、事件或其他方式)它不会从“this”访问任何东西,从实例被删除开始?