アプリケーションのすべてのリソースを管理する単一の「リソース モジュール」クラスがあり、主にすべてが存在する単一のファイル「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 は常に 1 つだけです。マルチスレッド セーフにしたいので、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 が互いに競合しますか?