recipes
recipes copied to clipboard
2.1.1 post和traverse示例问题
你好,买了你的书,不错啊! 但有个地方不太明白,你在2.1.1使用了shared_ptr::unique来断言g_foos是否为FooList的唯一引用,如果不是唯一引用,则重置。如下: void post(const Foo& f) { printf("post\n"); getlock locker(mutex); if (!g_foos.unique()) { g_foos.reset(new FooList(*g_foos)); printf("copy the whole list\n"); } assert(g_foos.unique()); g_foos->push_back(f); } 但是,我在https://zh.cppreference.com/w/cpp/memory/shared_ptr/unique这个文档中看到一段话: “此函数于 C++17 中被弃用并于 C++20 中移除,因为 use_count 在多线程环境中只是估计”。 请问如果我参考了你的解决方案,应用在项目中,后续是否有问题? 谢谢!
shared_ptr::use_count了解一下