W
Wake up Brazil
I've been told that this code shows undefined behavior because of §3.6.3/4 in the C++11 Standard, but I don't understand this assertion.
#include <iostream>
#include <thread>
class A
{
public:
void operator()() const { std::cout << 'A' << '\n'; }
};
int main()
{
std::thread t(A{});
t.detach();
}
The problem here is that cout may be used in the thread t after the end of main().
My interpretation of §3.6.3/4 (applied to cout) is as follows: if there is a use of cout in a signal handler that does not happen before completion of destruction of objects with static storage duration ..., the program hasundefined behavior.
But the code above doesn't use cout in a signal handler. Thus, AFAIK this paragraph doesn't apply in this case.
#include <iostream>
#include <thread>
class A
{
public:
void operator()() const { std::cout << 'A' << '\n'; }
};
int main()
{
std::thread t(A{});
t.detach();
}
The problem here is that cout may be used in the thread t after the end of main().
My interpretation of §3.6.3/4 (applied to cout) is as follows: if there is a use of cout in a signal handler that does not happen before completion of destruction of objects with static storage duration ..., the program hasundefined behavior.
But the code above doesn't use cout in a signal handler. Thus, AFAIK this paragraph doesn't apply in this case.