Danicco
Danicco

Reputation: 1673

Multithread ifstream/ofstream

I have a single "Resource Module" class that manages all resources for my application, and it reads mainly from a single file "RESOURCES.DAT" where everything is.

All objects that request new data from the file go through the ResourceModule so I can manage memory and avoid duplicate resources.

void SomeFunction()
{
    Image* newImage = new Image();
    newImage->Load("imageName");
}

void Image::Load(string imageName)
{
    //Pointer to Image Data
    _myImage = ResourceModule::GetResource(imageName);
}

There's always only one ResourceModule. I want to make it multithread safe, so when the GetResource(string resourceName) is called, it doesn't bug out.

If I do this:

Image* ResourceModule::GetResource(string imageName)
{
    ifstream fileReader;
    fileReader.open("RESOURCES.DAT", ios::binary);
    if(fileReader.is_open())
    {
        //Do the reading, return the pointer
    }
}

Is this multithread safe? Do multiple ifstreams/ofstreams conflict with each other when I declare them like this and read from the same file?

Upvotes: 1

Views: 1897

Answers (1)

dzada
dzada

Reputation: 5854

No

it will work as it is read only, every instance of ifstream will read. But that will not be a problem. Every ifstream will have it's own position in the file, and progress in parallel.

You don't need to do anything

Upvotes: 2

Related Questions