我需要对以下代码块进行一些澄清.我正在研究类似编码的东西,我想了解它是如何工作的.谷歌上没有太多的提及,我发现的唯一一个技术太难以理解了.
在以下代码中,线程和非线程函数具有不同的输出.这是为什么?
#include <boost/thread.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
void fTry1(const char&);
void fTry2(const char&);
int main()
{
boost::thread tTry1, tTry2;
const char *a = "efgh";
tTry1 = boost::thread(fTry1, *a);
fTry2(*a);
tTry1.join();
return 0;
}
void fTry1(const char& a)
{
cout << "Thread" << endl;
cout << &a << endl;
}
void fTry2(const char& a)
{
cout << "Non-thread" << endl;
cout << &a << endl;
}
Run Code Online (Sandbox Code Playgroud)样本输出:
Non-thread
efgh
Thread
e<garbage data>
Run Code Online (Sandbox Code Playgroud)
如果我改变以下行
cout << &a << …Run Code Online (Sandbox Code Playgroud)