我有两个类似的类:
class Foo {
public:
void bar() {
std::lock_guard<std::mutex> lock(m_mutex);
m_data.push_back('x');
}
private:
std::string m_data;
std::mutex m_mutex;
};
class Pool {
public:
static std::shared_ptr<Foo> Create(int index) {
std::lock_guard<std::mutex> lock(m_mutex);
if (m_pool.size() > 10) {
m_pool.erase(m_pool.begin());
}
std::shared_ptr<Foo>& ptr = m_pool[index];
if (!ptr) ptr.reset(new Foo);
return ptr;
}
private:
static std::mutex m_mutex;
static std::map<int, std::shared_ptr<Foo>> m_pool;
};
和运行这段代码的几个线程:
void parallel_function(int index) {
// several threads can get the same index
std::shared_ptr<Foo> foo = Pool::Create(index);
foo->bar();
}
All member functions (including copy constructor and copy assignment) can be called by multiple threads on different instances of shared_ptr without additional synchronization even if these instances are copies and share ownership of the same object. If multiple threads of execution access the same shared_ptr without synchronization and any of those accesses uses a non-const member function of shared_ptr then a data race will occur; the shared_ptr overloads of atomic functions can be used to prevent the data race.
两个问题:
由于
Pool::Create
总是返回shared_ptr
的拷贝,我假设每个shared_ptr
的拷贝和销毁是线程安全,如果它发生在m_pool.erase
中或发生在parallel_function
的末尾。这是正确的吗?我调用了
shared_ptr::operator->
,它是一个 const 成员函数,函数Foo::bar
是线程安全的。这里有数据竞赛吗?
最佳答案
总结一下我的意见。
- 是的,这是线程安全的,因为您在不同线程中操作
shared_ptr
的单独拷贝。这是传递shared_ptr
的拷贝实际上是合理的少数情况之一。 operator->
是一个 const member .所以基本上你的代码是好的,只要 Foo::bar 是无竞争的(现在显然是这样)。
关于c++ - 多线程的 shared_ptr 销毁是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32750945/