您的位置:首页 > 其它

boost的原子操作

2015-06-03 15:50 281 查看
[cpp] view
plaincopy





int a=0;  

std::cout<<a<<std::endl;  

boost::thread t1([&](){  

      

    for (int cnt=0;cnt<100000;cnt++)  

    {  

        a+=1;  

    }  

  

});  

boost::thread t2([&](){  

    for (int cnt=0;cnt<100000;cnt++)  

    {  

        a-=1;  

    }  

  

});  

t1.join();  

t2.join();  

std::cout<<'\t'<<a<<std::endl;  

输出:

-3529

[cpp] view
plaincopy





boost::atomic_int a(0);  

std::cout<<a<<std::endl;  

boost::thread t1([&](){  

      

    for (int cnt=0;cnt<100000;cnt++)  

    {  

        a+=1;  

    }  

  

});  

boost::thread t2([&](){  

    for (int cnt=0;cnt<100000;cnt++)  

    {  

        a-=1;  

    }  

  

});  

t1.join();  

t2.join();  

std::cout<<'\t'<<a<<std::endl;  

输出

0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息