Multithread ifstream/ofstream
Multithread ifstream/ofstream
我有一个单独的"资源模块"类来管理我的应用程序的所有资源,它主要从一个文件"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(字符串资源名称)时,它不会出错。
如果我这样做:
Image* ResourceModule::GetResource(string imageName)
{
ifstream fileReader;
fileReader.open("RESOURCES.DAT", ios::binary);
if(fileReader.is_open())
{
//Do the reading, return the pointer
}
}
这个多线程安全吗?当我这样声明多个ifstream/ofstreams并从同一文件中读取时,它们是否相互冲突?
无
它将以只读方式工作,ifstream的每个实例都将读取。但这不会成为问题。每个ifstream在文件中都有自己的位置,并且并行进行。
你不需要做任何事情
相关文章:
- Ofstream和IFSTREAM有什么区别?
- C++:std::ofstream 方法 open() 在第二次迭代时擦除打开的 ifstream 文件
- fstream / ifstream / ofstream 对象如何转换为布尔值
- Using ifstream, ofstream and fstream
- C++ 中的 ifstream 和 ofstream 出错
- C++无法在函数内检查 ifstream/ofstream.is_open()
- ofstream和ifstream路径的作品写作不阅读C 构建器
- ifstream和ofstream:如何对文件进行多次修改
- 如何提高 ifstream 和 ofstream 的读写速度 std::vector<uint8_t>
- 使用套接字编程的c++中的字符串流与ifstream(ofstream)
- 这里不允许函数定义:void encryption(ifstream encrypt_file,ofstream key
- fstream、ifstream和ofstream的开放模式行为
- 写入 ofstream,然后使用 ifstream 读取不会读取整个文件
- std::ifstream::read 或 std::ofstream::write 带有零参数?
- ifstream/ofstream真的用于序列化吗
- 常见的父类型 ifstream 和 ofstream
- 为什么'ifstream'和'ofstream'被添加到"std",而'fstream'可以同时达到这两个目的?
- 带有ifstream和ofstream的变量文件名
- 读取文件(ifstream)和写入文件(ofstream)-编码问题
- Xcode 5 c++ fstream(ifstream 和 ofstream)不起作用