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