我们提供了一个与许多其他 API 不是线程安全的包进行交互的包。我们的包的 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/