代码改变世界

共享节

2012-11-01 09:31  龙成  阅读(308)  评论(0编辑  收藏  举报
#include <iostream>
using namespace std;

/*创建自定义的数据节*/
#pragma data_seg("Shared")

bool isExist = false; /*已经初始化变量,在节中,可共享*/
int num1; /*未初始化变量,不在节中,不可共享*/

#pragma data_seg()

/*添加初始化的变量,在节中,可共享*/
__declspec(allocate("Shared")) int num2 = 0;

/*添加未初始化的变量,在节中,可共享*/
__declspec(allocate("Shared")) int num3;

int num4 = 0; /*普通全局变量,无法共享*/

/*设置共享数据节的属性*/
#pragma comment(linker, "/Section:Shared,RWS")
void main()
{
if(!isExist)
{
isExist = true;
num1 =1;
num2 = 2;
num3 = 3;
num4 = 4;
cout << "This is the first instance !" << endl;
}
else
{
cout << "This is not the first instance !" << endl;
}
cout << "num1 : " << num1 << endl;
cout << "num2 : " << num2 << endl;
cout << "num3 : " << num3 << endl;
cout << "num4 : " << num4 << endl;
system("pause");
}