问题描述
|
最近我想到了一个狡猾的计划(tm:P))
我必须在程序中更新设置结构(每15秒说一次)。设置结构由多个功能使用,并且每个功能均由多个线程调用。
因此,我需要一个参考计数器来知道何时可以安全地释放旧设置结构。
那么这是正确的方法吗?
如果您没有仔细阅读代码,请不要回应它是可以的,当涉及到共享指针时,在进行此类滥用时很容易出错(请相信我)。
编辑:我忘了提到重要的部分。我认为该实现可防止ref计数器下降到0,因为我在updateSettings()中对其进行了初始化,并且直到再次调用它时它才会下降(然后myFucntion使用内存中的2个设置中的另一个)。
#include<memory>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
struct STNGS
{
int i;
vector<double> v;
};
static int CUR_STNG=0;
shared_ptr<STNGS> stngsArray[2];
int myFunction() //called by multiple threads
{
shared_ptr<STNGS> pStngs=stngsArray[CUR_STNG];
STNGS& stngs=*pStngs;
//do some stuff using stngs
}
void updateSettings()
{
auto newIndex=(CUR_STNG+1)%2;
stngsArray[newIndex].reset(new STNGS);
CUR_STNG=newIndex;
}
void initialize()
{
auto newIndex=CUR_STNG;
stngsArray[newIndex].reset(new STNGS);
CUR_STNG=newIndex;
}
int main()
{
initialize();
//launch bunch of threads that are calling myFunction
while(true)
{
//call updateSettings every 15 seconds
}
}
编辑:使用来自评论的反馈我更新了代码:
#include<memory>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
static const int N_STNG_SP=4;
static int CUR_STNG=0;
struct STNGS
{
int i;
vector<double> v;
STNGS()
{
for (int i=0;i<10;++i)
v.push_back(42);
}
};
shared_ptr<STNGS> stngs[N_STNG_SP];
int myFunction() //called by multiple threads
{
shared_ptr<STNGS> pStngs=stngs[CUR_STNG];
STNGS& stngs=*pStngs;
//do some stuff using stngs
}
void updateSettings()
{
auto pStng=new STNGS;
//fill *pStng
int newVer=(CUR_STNG+1)%N_STNG_SP;
stngs[newVer].reset(pStng);
CUR_STNG=newVer;
}
void initialize()
{
auto pStng=new STNGS;
//fill *pStng
int newVer=(CUR_STNG+1)%N_STNG_SP;
stngs[newVer].reset(pStng);
CUR_STNG=newVer;
}
int main()
{
initialize();
//launch bunch of threads that are calling myFunction
while(true)
{
//call updateSettings every 15 seconds
updateSettings();
}
}
解决方法
我不相信这个代码。我相信,除了两个引用计数外,它在不同线程共享的所有内存上都没有适当的内存屏障。
在我看来,这似乎是shared_mutex的一个很好的应用程序。
编辑:
20.7.2.2 [util.smartptr.shared] / p4说:
为了确定
存在数据竞赛,成员
功能只能访问和修改
shared_ptr和weak_ptr对象
自己而不是引用的对象
至。
但是,代替使用shared_mutex,另一种选择可能是在20.7.2.5 shared_ptr原子访问[util.smartptr.shared.atomic]中使用API:
并发访问shared_ptr
来自多个线程的对象不
如果访问是
仅通过中的功能完成
本节和实例是
通过作为他们的第一个论点。
template<class T>
bool atomic_is_lock_free(const shared_ptr<T>* p);
template<class T>
shared_ptr<T> atomic_load(const shared_ptr<T>* p);
template<class T>
shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p,memory_order mo);
template<class T>
void atomic_store(shared_ptr<T>* p,shared_ptr<T> r);
template<class T>
void atomic_store_explicit(shared_ptr<T>* p,shared_ptr<T> r,memory_order mo);
template<class T>
shared_ptr<T> atomic_exchange(shared_ptr<T>* p,shared_ptr<T> r);
template<class T>
shared_ptr<T>
atomic_exchange_explicit(shared_ptr<T>* p,memory_order mo);
template<class T>
bool
atomic_compare_exchange_weak(shared_ptr<T>* p,shared_ptr<T>* v,shared_ptr<T> w);
template<class T>
bool
atomic_compare_exchange_strong( shared_ptr<T>* p,shared_ptr<T> w);
template<class T>
bool
atomic_compare_exchange_weak_explicit(shared_ptr<T>* p,shared_ptr<T> w,memory_order success,memory_order failure);
template<class T>
bool
atomic_compare_exchange_strong_explicit(shared_ptr<T>* p,memory_order failure);
shared_mutex将更容易正确。但是原子的shared_ptr API可能会产生更高性能的解决方案。
更新:
这是shared_mutex解决方案的未经测试的代码(请注意,shared_mutex不是std,但它是第3方库):
struct STNGS
{
int i;
vector<double> v;
ting::shared_mutex m;
};
STNGS stngs;
int myFunction() //called by multiple threads
{
shared_lock<shared_mutex> _(stngs.m);
//do some stuff using stngs
return 0;
}
void updateSettings()
{
unique_lock<shared_mutex> _(stngs.m);
//fill stngs
}
void initialize()
{
//fill stngs
}
这是未经测试的代码,它为shared_ptr使用原子加载/存储功能:
struct STNGS
{
int i;
vector<double> v;
};
shared_ptr<STNGS> pStng;
int myFunction() //called by multiple threads
{
shared_ptr<STNGS> stngs = atomic_load(&pStng);
//do some stuff using *stngs
return 0;
}
void updateSettings()
{
shared_ptr<STNGS> newStng(new STNGS);
//fill *newStng
atomic_store(&pStng,newStng);
}
void initialize()
{
pStng.reset(new STNGS);
//fill *pStng
}