When does the main exit go to the console exit?

#include<iostream>
#include<thread>
using namespace std;
void func()
{
    for (int i = 0; i < 10000; i++)cout << "Print" << endl;
}

int main()
{
    thread t(func);
    t.detach();
    cout << "Exit" << endl;
    return 0;
}

In the above code, when the main outputs, where "Print"does the text disappear because it does not have an output stream? Is there any dummy thread to insert data that is not in use?

+4
source share
1 answer

When mainthe call completes, it calls exitwhich terminates all threads, regardless of whether it is disconnected or not. This is because it exitcompletes the whole process.

The C ++ runtime runs mainas exit(main(argc, argv)), so returning from maina call exit.

, , pthread_exit. main exit. , - exit ( ). Linux, Windows.

std::cout, , exit. Schwarz Counter, , , . , , , main ( exit), - . , , , ( ) .

ISO/IEC 14882: 2011 (E) :

27.4 iostream

27.4.1.2 [ ] , ios_base::Init main †. . ios_base::Init . , ios_base::Init .

stdin stdout stderr.

+5

All Articles