我回到c++编程,因此,我正在尝试许多事情来掌握新的c++标准。我知道我提供的代码非常糟糕。我想我知道如何解决它。
代码:
#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;
}
一种输出:
!!!Hello World!!!
aaa
aaa
从代码中,我们可以看到“aaa”应该被打印一次。
我的问题是为什么输出上有两个“aaa”。是什么原因导致此问题?
非常感谢
编辑:
添加的系统信息:
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)
其他一些输出:
类型A:
!!!Hello World!!!
aaaaaa
B型:
!!!Hello World!!!
typeC:
!!!Hello World!!!
aaaaaa
typeD:
!!!Hello World!!!
aaa
<empty line>
编辑:
根据@mark的答案。这是未定义的行为。
我添加了“sleep(1);”在主要返回之前。它只给我一种输出类型。现在我很困惑,如果它是未定义的,为什么我没有看到其他类型的输出?
输出:
!!!Hello World!!!
aaa
最佳答案
除非您使用的是真正的c++ 11编译器,否则std::cout
并非线程安全的。没有保护,您不能有2个线程写入cout。如果这样做,您将得到不确定的行为-就像您看到的一样!
查看此讨论:Is cout synchronized/thread-safe?
在gcc 4.7之前,似乎不完全支持c++ 11,并且您具有gcc 4.6
关于multithreading - c++ 11线程未知输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18772867/