首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

不用static,怎么实现如下计数程序

2012-02-16 
不用static,如何实现如下计数程序?不用static,如何实现如下计数程序?staticintm_couter0if(m_couter%8

不用static,如何实现如下计数程序?
不用static,如何实现如下计数程序?
static   int   m_couter   =   0;
if   (m_couter%8   ==   0)
{
.......
}
m_couter++;

[解决办法]
? 不用 static 那就全局变量咯
[解决办法]
方法1:全局变量
#include <iostream>
int i=0;

void fun() {++i;};

int main()
{
fun();
std::cout < <i < <std::endl;
}
输出为:1
[解决办法]
使用Thread Local Storage。
比如:
#define ThreadSafe __declspec( thread )
ThreadSafe int counter;

用ThreadSafe去修饰你的全局变量,你的全局变量就变得线程安全了。

热点排行