读书人

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

发布时间: 2012-02-16 21:30:36 作者: rapoo

不用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去修饰你的全局变量,你的全局变量就变得线程安全了。

读书人网 >C++

热点推荐