如何解决std::shared_ptr<std::mutex> 的线程安全
考虑以下带有 uriParameters:
page:
required: true
displayName: page
description: page
type: integer
default: 0
example:
0
size:
required: true
displayName: size
description: size
type: integer
default: 100
example:
100
数据对象的 class
。目的是获取廉价的可复制对象,并在副本之间共享数据缓存。
shared_ptr
对 #include <memory> // std::shared_ptr
#include <mutex> // std::mutex,std::lock_guard
class DataClass {
std::shared_ptr<DataType> data;
std::shared_ptr<std::mutex> data_lock;
public:
DataClass() : data(std::make_shared<DataType>()),data_lock(std::make_shared<std::mutex>()) {}
DataType get_data() const {
std::lock_guard<std::mutex> lock_guard(*data_lock);
if (/* data not cached */) {
/* calculate and store data */
}
return *data;
}
};
的调用是线程安全的吗?如果没有,如何实现?
我确实有一些代码似乎以死锁告终(太大/太复杂而无法发布),它依赖于上述内容在并行计算中正常工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。