锁到底有什么用处?
我下面的程序,大意是两个线程同时修改同一个对象的成员,没有使用锁,也没有出现错误阿
锁的作用不是防止多个线程同时访问同一个成员出错设置的么?为什么这里不需要?
class test
{
public:
test() : value(0)
{}
~test(){}
int value;
};
class MyThread
{
public:
int ThreadProc(void *param){
test *ptt = (test*)param;
while(1)
{
ptt->value = 100;
}
return 0;
}
};
int main()
{
test *pt = new test;
MyThread *thd = new MyThread;
thd->Run(pt);
MyThread *thd2 = new MyThread;
thd2->Run(pt);
while(1){
Sleep(1000);
}
return 0;
}