gpt4 book ai didi

c++ - 用于公开缓存数据的同步线程安全 API

转载 作者:搜寻专家 更新时间:2023-10-31 01:32:11 25 4
gpt4 key购买 nike

我们提供了一个包,它与其他 API 不是线程安全的包接口(interface)。我们包的 API 完全基于消息,因此是异步的,以便为我们包的用户提供线程安全。因此,我们的包包装了一堆非线程安全的包,并提供了一个线程安全的 API。这意味着我们包的用户可以从任何线程与我们的包进行交互。

我们希望在保持线程安全的同时向我们的包中添加同步 API。我做了一些研究,并提出了两种可能的模式来执行此操作,我将在下面分享。我对这两种方法都不满意,所以想知道社区是否可以对我们可以使用的模式提出更多建议。请注意,下面的代码用于设计和说明目的(C++ 伪代码),因此不能编译。

方法 1 - 包用户依赖项将数据访问类注入(inject)到我们的包中。我们使用运行时类型推断访问这些类。

// Define an interface class for all data accessor classes
class DataAccessor
{

}

// Some random data
class FooData
{
int foo;
int bar;
}

// A concrete data accessor
class FooDataAccessor : public DataAccessor
{
public:
FooData getFooData()
{
FooData fooDataCopy;
{
//Locks cachedFooData in this scope
ScopedCriticalSection _(dataCriticalSection);
fooDataCopy.foo = cachedFooData.foo;
fooDataCopy.bar = cachedFooData.bar;
}
return fooDataCopy;
}
void setFooData(FooData& fooData)
{
//Locks cachedFooData in this scope
ScopedCriticalSection _(dataCriticalSection);
cachedFooData.foo = dooData.foo;
cachedFooData.bar = dooData.bar;
}

private:
FooData cachedFooData;
CriticalSection dataCriticalSection; //Use this for locking cachedFooData to set the data.
}

class OurPackage
{
OurPackage(std::vector<DataAccessor*>); //constructor which is injected the data accessors so that our package customers can own their lifecycle.
}

// How package users would inject the data accessors into our package, then later access the data
int main()
{
std::vector<DataAccessor*> dataAccessors;
//The package customer now populates the data Accessors with the instances they need.
dataAccessors.push_back(new FooDataAccessor());

auto package = new OurPackage(dataAccessors);

// How package users access the data, assume FooDataAccessor was at the front
auto fooAccessor = dataAccessors.front();
if (fooAccessor)
{
FooData data = fooAccessor->getFooData();
}
}

// In OurPackage, set the actual data in caches
for (DataAccessor* dataAccessor : dataAccessors)
{
//Use RTTI to find the instance we want
if (auto dataAccessorTypeWeWant = dynamic_cast<DataAccessorTypeWeWant*>(dataAccessor) != nullptr)
{
//Set the data on dataAccessorTypeWeWant
//For example, set FooData
FooData fooData;
fooData.foo = 1;
fooData.bar = 2;
dataAccessorTypeWeWant.setFooData(fooData);
break;
}
}

2 - Use a singleton pattern instead

如果数据访问缓存是单例的,包用户就不必管理这些类的生命周期,也不必担心将指针传递给应用程序周围的数据访问缓存实例。不过,这具有单例的所有缺陷。

最佳答案

无论您选择什么模式,您都应该使用库中创建的原子类型 <atomic> ,自 C++11 起可用的功能。使用这种类型,您可以创建线程安全变量,例如:

// Some random data
class FooData
{
std::atomic<int> foo;
std::atomic<int> bar;
}

我从 CPlusPlus 向您分享了这个库的描述:

Atomic types are types that encapsulate a value whose access is guaranteed to not cause data races and can be used to synchronize memory accesses among different threads.

关于c++ - 用于公开缓存数据的同步线程安全 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43814931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com