#pragma data_seg("Share")
volatile int g_nFirst = 0;
int g_nSecond; //注意这里没有初始化,不能放入共享区
#pragma data_seg()
__declspec(allocate("Share")) int g_nThird = 0; //通过__declspec(allocate放入共享区
__declspec(allocate("Share")) int g_nFouth; //可以不进行初始化放入
#pragma comment(linker, "/Section:Share,RWS") //一定要 加上这句, R表示可读, W可写,S共享
int _tmain(int argc, _TCHAR* argv[])
{
g_nFirst++;
if (g_nSecond < 0)
{
g_nSecond = 0;
}
else
{
g_nSecond++;
}
g_nThird++;
if (g_nFouth < 0)
{
g_nFouth = 0;
}
else
{
g_nFouth++;
}
printf("%d, %d, %d, %d", g_nFirst, g_nSecond, g_nThird, g_nFouth);
getchar();
return 0;
}
本文介绍了一种在C++中使用共享内存区域的方法,并详细解释了如何通过不同的声明方式将变量放入共享段。同时,文章提供了示例代码,展示了如何初始化这些变量以及如何在程序中进行操作。
1095

被折叠的 条评论
为什么被折叠?



