网站建设大概费用,奥美广告公司,国外购物网站欣赏,p2p网站开发的多少钱通过《Modern C std::shared_ptr的实现原理》我们看到引用计数和weak计数在链接pthread的情况下都是原子操作#xff0c;而不链接的情况必是单线程也没有竞争存在。 可以看到shared_ptr本身只读的情况下是线程安全的#xff0c;但是有两种情况不怎么安全#xff1a;
通过它…通过《Modern C std::shared_ptr的实现原理》我们看到引用计数和weak计数在链接pthread的情况下都是原子操作而不链接的情况必是单线程也没有竞争存在。 可以看到shared_ptr本身只读的情况下是线程安全的但是有两种情况不怎么安全
通过它操作被管理的对象就必须自己上锁以保证没有数据竞争了。同一个shared_ptr对象被多个线程指向或引用此时这个shared_ptr对象本身成了共享资源如果存在一个线程修改shared_ptr对象本身比如调用reset则不安全。请参考另一篇分析reset源码的帖子
本节着重第一种情况请看下面的程序
#include iostream
#include memory
#include threadvoid shared_ptr_example() {std::shared_ptrint sharedInt std::make_sharedint(42);// Multiple threads increment the shared integerstd::thread t1([sharedInt]() {for (int i 0; i 1000000; i) {// Accessing and modifying the shared data(*sharedInt);}});std::thread t2([sharedInt]() {for (int i 0; i 1000000; i) {// Accessing and modifying the shared data(*sharedInt);}});t1.join();t2.join();// Output may vary due to the lack of synchronizationstd::cout Final value: *sharedInt std::endl;
}int main() {shared_ptr_example();return 0;
}在我机器上结果如下
[mzhaic]$ ./a.out
Final value: 1385208
[mzhaic]$ ./a.out
Final value: 2000042
[mzhaic]$ ./a.out
Final value: 1375690每次结果都不一样。所以必定存在数据竞争。