cppython
cppython

Reputation: 1279

c++11 thread unknown output

I return to c++ programming, therefore, I am trying a lot of things to master new c++ standard. I know the code I provided is very bad. I think I know how to fix it.

code:

#include <iostream>
#include <thread>

using namespace std;

void apple (string const& x)
{
                cout << "aaa" << endl;
}

void orange()
{
                //string s = "hello";
                thread t(apple,"hello");
                t.detach();
}

int main() {
                orange();
                cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
                return 0;
}

one kind of outputs:

!!!Hello World!!!
aaa
aaa

from the code, we can see the "aaa" should be printed once. my question is why there are two "aaa" on the output. what cause this problem?

many thanks

edit: added system info:

Using built-in specs.
COLLECT_GCC=/usr/bin/g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.6.3/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.6.3 20120306 (Red Hat 4.6.3-2) (GCC) 

some other outputs:

type A:

!!!Hello World!!!
aaaaaa

typeB:

!!!Hello World!!!

typeC:

!!!Hello World!!!
aaaaaa

typeD:

!!!Hello World!!!
aaa
<empty line>

edit: according @mark's answer. this is an undefined behavior.
I added a "sleep(1);" before main return. it gives me only one type of output. now i got confused, if it is undefined, why i didnt see other types of output?

output:

!!!Hello World!!!
aaa

Upvotes: 1

Views: 154

Answers (1)

Mark Lakata
Mark Lakata

Reputation: 20838

std::cout is not thread safe unless you are using a real c++11 compiler. You can't have 2 threads writing to cout without protection. If you do, you will get undefined behavior -- just like you see!

See this discussion: Is cout synchronized/thread-safe?

c++11 does not appear to be fully supported until gcc 4.7, and you have gcc 4.6

Upvotes: 3

Related Questions