请阅读如下代码(all.h,test1.cpp,test2.cpp,main.cpp),请问对main函数中的func1和func2来说,g_str是否是全局变量?对main中thread_group而言,g_str是否是全局变量?
两个小问题,欢迎参与哈:)
all.h
#ifndef ALL_H
#define ALL_H
#include <iostream>
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/thread/thread.hpp>
using namespace std;
using namespace boost;
static char g_str[] = "123456";
void fun1( );
void fun2( );
#endif /* ALL_H */
test1.cpp
#include "all.h"
void print2() {
//g_str[1]='P';
cout <<"func1: " <<g_str << " ADDR: " << &g_str << endl;
}
void change12() {
g_str[1] = 'm';
}
void change22() {
g_str[1] = 'n';
}
void fun1() {
//g_str[1]='P';
print2();
change12();
print2();
change22();
print2();
}
test2.cpp
#include "all.h"
void print() {
//g_str[1]='P';
string tid=lexical_cast<string>(boost::this_thread::get_id());
cout <<"func2: " <<tid<<" "<<g_str << " ADDR: " << &g_str << endl;
}
void change1() {
g_str[1] = 'x';
}
void change2() {
g_str[1] = 'y';
}
void fun2() {
//g_str[1]='P';
print();
change1();
print();
change2();
print();
cout<<"------"<<endl;
}
main.cpp
/*
* File: main.cpp
* Author: hcx
*
* Created on September 3, 2013, 9:03 PM
*/
#include <cstdlib>
#include "all.h"
//#include "t1.h"
//#include "t2.h"
int main(int argc, char** argv) {
//--------------------------------------
fun1();
fun2();
//--------------------------------------
int num;
thread_group threads;
num = 4;
cout << "我是主程序,我准备产生" << num << "个子线程" << endl;
for (int i = 0; i < num; i++) {
threads.create_thread(bind(&fun2));
}
cout << "我是主程序,我在等子线程运行结束" << endl;
threads.join_all();
return 0;
}