有两个类
class TestClass
{
public:
TestClass();
map<int,int> int_map;
};
class TestClass2
{
public:
TestClass2(TestClass* tc);
void Run();
TestClass* tc1;
};
TestClass::TestClass()
{
printf("TestClass1 addr :%p \n",this);
}
TestClass2::TestClass2(TestClass* tc){
tc1 = tc;
printf("TestClass2 get TestClass1 ,addr :%p !\n",tc1);
}
void TestClass2::Run(){
int i = 0;
while(true){
tc1->int_map.insert(make_pair(i,1));
printf("TestClass2:size of TestClass1 %p 's int_map is %d!\n",tc1,tc1->int_map.size());
sleep(2);
i++;
}
}
int main()
{
TestClass* tc1 = new TestClass();
printf("tc1 addr is %p!\n",tc1);
TestClass2* tc2 = new TestClass2(tc1);
pid_t pid = fork();
if(pid == 0){
while(1){
sleep(1);
printf("TestClass1 :%p int_map size %d!\n",tc1,tc1->int_map.size());
}
}else{
tc2->Run();
}
}
TestClass2 初始化后获得TestClass对象的指针,
通过这个指针来访问TestClass中的map,
每次访问后打印size
两个类在不同的线程打印,按理说size应该一样,
结果TestClass2可以输出数量,而TestClass自己本身却输出0
fork()
是派生一个子进程和线程没啥关系,而子进程拷贝了一份父进程环境变量和对象信息,已经和父进程的没啥关系了; c++11提供了线程库,直接取用结果如下: