P
P
pixik2016-08-06 23:24:09
C++ / C#
pixik, 2016-08-06 23:24:09

Why is the same line sometimes printed in a separate C++ thread?

Why was the unit printed twice in the 3rd line?

void foo(){
    for(int i = 0; i < 5; i++){
        cout << "cout " << i << endl;
    }
}
int main() {
    thread t(foo);
    t.detach();
    assert(!t.joinable());
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

output is as follows (one of many options):
Hello, World!cout 
0
cout 1cout 1
cout 2
c

Answer the question

In order to leave comments, you need to log in

1 answer(s)
#
#algooptimize #bottize, 2016-08-07
@user004

most likely due to the effect of flush when calling < std::endl

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question