我们提供了一个包,它与许多API不是线程安全的其他包接口。我们的包的API完全基于消息,因此是异步的,以允许我们包的用户的线程安全。因此,我们的包包装了一堆非线程安全的包,并提供了一个线程安全的API。这意味着我们的包的用户可以从任何线程与我们的包进行交互。
我们希望在保持线程安全的同时为我们的包添加同步API。我做了一些研究,并提出了两种可能的模式,我将在下面分享。我对这两种方法都不满意,所以我想知道社区是否可以对我们可以使用的模式提出更多建议。请注意,下面的代码用于设计和说明目的(c ++伪代码),因此无意编译。
方法1 - 打包用户依赖注入数据访问类到我们的包。我们使用运行时类型推断来访问这些类。
// 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
如果数据访问缓存是单例,则包用户不必管理这些类的生命周期,也不必担心将指针传递给应用程序周围的数据访问缓存实例。这有单身人士的所有陷阱。
答案 0 :(得分:3)
无论您选择何种模式,您都应该使用库<atomic>
中的原子类型,这是自C ++ 11以来可用的功能。使用此类型,您可以创建线程安全变量,例如:
// Some random data
class FooData
{
std::atomic<int> foo;
std::atomic<int> bar;
}
我从CPlusPlus分享了这个库的描述:
原子类型是封装一个值的类型,该值的访问保证不会导致数据争用,并且可用于同步不同线程之间的内存访问。
答案 1 :(得分:0)
这是一个样本。如果您需要按值复制。
#include <vector>
#include <iostream>
#include <thread>
#include <atomic>
#include <mutex>
// Undefine to see data corruptions
#define USING_LOCK
std::atomic<int> atomic_i;
class FooData {
public:
FooData() :foo(atomic_i.fetch_add(1, std::memory_order_relaxed)), bar(foo) {}
~FooData() { if (foo != bar) { std::cout << "Data corrupted!\n"; } }
private:
int foo;
int bar;
};
class FooDataAccessor {
public:
FooData getFooData() {
#ifdef USING_LOCK
std::lock_guard<std::mutex> l(_lock);
#endif // USING_LOCK
return cachedFooData;
}
void setFooData(const FooData& fooData) {
#ifdef USING_LOCK
std::lock_guard<std::mutex> l(_lock);
#endif // USING_LOCK
cachedFooData = fooData;
}
private:
FooData cachedFooData;
#ifdef USING_LOCK
std::mutex _lock;
#endif // USING_LOCK
};
void f(FooDataAccessor* accessor) {
for (size_t i = 0; i < 1000; i++) {
accessor->getFooData();
accessor->setFooData(FooData());
}
}
int main() {
FooDataAccessor accessor;
std::vector<std::thread> v;
for (size_t i = 0; i < 5; i++) {
v.emplace_back(f, &accessor);
}
for (auto& t : v) {
t.join();
}
}