这个函数(运算符重载)是线程安全的吗?
这是代码:
ElementType& operator[] (int key)
{
//something like boost::mutex::scoped_lock
MutexLockType lock();
if(key < 0 || key > m_bound)
throw std::range_error("access out of bound");
return m_elements[key];
}
Here's the code:
ElementType& operator[] (int key)
{
//something like boost::mutex::scoped_lock
MutexLockType lock();
if(key < 0 || key > m_bound)
throw std::range_error("access out of bound");
return m_elements[key];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,这不是因为您允许元素的引用从函数中泄漏并远离锁的安全性。
更重要的是,如果没有更严格的规范,线程安全问题就有点难以回答 您最想要哪种类型的线程安全。至少您需要向我们展示
m_elements
和m_bound
的所有其他访问权限。No it is not because you have allowed a reference the element to leak out of the function and away from the safety of the lock.
What's more, thread-safety questions are a little hard to answer without a harder specification of just what flavour of thread-safety you are desirest. At the very least you would need to show us every other access of
m_elements
andm_bound
.在这个确切的示例中,当您意识到根本没有锁,只有返回 LockType 的 lock() 函数的声明时,您会更加惊讶。
无论如何,这并不是说它会对锁有帮助。
In this exact example you would be even more surprised when you realize that there is no lock at all, just a declaration of a lock() function that returns a LockType.
Not that that it would have helped with a lock anyway.