This is a scenario you shouldn't ever do, but https://timsong-cpp.github.io/cppwp/class.cdtor#4 states:
Member functions, including virtual functions ([class.virtual]), can be called during construction or destruction ([class.base.init]).
Does this hold if the functions are called in parallel? That is, ignoring the race condition, if the A
is in the middle of construction, and frobme
is called at some point AFTER the constructor is invoked (e.g. during construction), is that still defined behavior?
#include <thread>
struct A {
void frobme() {}
};
int main() {
char mem[sizeof(A)];
auto t1 = std::thread([mem]() mutable { new(mem) A; });
auto t2 = std::thread([mem]() mutable { reinterpret_cast<A*>(mem)->frobme(); });
t1.join();
t2.join();
}
As a separate scenario, it was also pointed out to me that it's possible for A
's constructor to create multiple threads, where those those threads may invoke a member function function before A
is finished construction, but the ordering of those operations would be more analyzable (you know no races will occur until AFTER the thread is generated in the constructor).