C ++ static variables always get destroyed in main thread?

Short question:

Are static (non thread_local) C ++ 11 variables always always destroyed in the main thread?

Are they always destroyed only upon exit from the program (given that we do not manually call their destructors)?

UPDATE

For brevity, suppose that ARE destructors are called. (we did not pull out the plug, we did not kill -9)

+6
source share
1 answer

Global object destructors are called std::exit. This function is called at C ++ runtime upon return main.

std::exit , , main. :.

struct A
{
    A() { std::cout << std::this_thread::get_id() << '\n'; }
    ~A() { std::cout << std::this_thread::get_id() << '\n'; }
};

A a;

int main() {
    std::thread([]() { std::exit(0); }).join();
}

:

140599080433472
140599061243648

, , - .

. std::exit std::atexit .

+11

All Articles