我有一个“资源模块”类来管理我的应用程序的所有资源,它主要从一个文件“ RESOURCES.DAT ”中读取所有内容。
从文件中请求新数据的所有对象都通过 ResourceModule,因此我可以管理内存并避免重复资源。
void SomeFunction()
{
Image* newImage = new Image();
newImage->Load("imageName");
}
void Image::Load(string imageName)
{
//Pointer to Image Data
_myImage = ResourceModule::GetResource(imageName);
}
总是只有一个 ResourceModule。我想让它多线程安全,所以当调用GetResource(string resourceName)时,它不会出错。
如果我这样做:
Image* ResourceModule::GetResource(string imageName)
{
ifstream fileReader;
fileReader.open("RESOURCES.DAT", ios::binary);
if(fileReader.is_open())
{
//Do the reading, return the pointer
}
}
这个多线程安全吗?当我这样声明它们并从同一个文件中读取时,多个 ifstreams/ofstreams 是否会相互冲突?