Thread-safe file input (c++) ?

Started by
4 comments, last by Rasmadrak 16 years, 6 months ago
Hi, I'm working out the kinks in my game-engine, and found what I believe to be the problem - file input. More precisly, when I'm using FILE* or ifstream/fstream across multiple threads (different threads using different files) my program crashes. If I load the objects separatly, one at a time, in different threads or not - everything works fine so theres no problems in the loading code ( I think!). If I try to dual-load two different objects - it crashes. Is this a common problem and/or has anyone got a solution to this? I've tried loading binary files and regular files, but all with the same outcome. Anyone? :) Thanks for your time! Cheers, Robert
"Game Maker For Life, probably never professional thou." =)
Advertisement
Manipulating streams or file pointers is not atomic.

So, if two threads interleave manipulation of the same stream or file pointer at the same time, they run the risk of destroying its internal state (and, by extension, crashing). So, keep your input-output in the same thread, or protect it with mutexes so that only one thread uses it at any point in time. And, by the way, the result of interleaved file input-output is in general a complete mess.

Conversely, if you're using distinct file pointers or streams to access the same file, then it's quite possible that one of them will get an OS-level lock on the file, which will prevent the other from opening it. Reading from a file without checking that you correctly opened it will also result in nasties.
Hmm...

So even if I declare a new fstream variable inside each thread, they still share one common stream?

I expected this problem when different threads would access the same file, but in this case they´re using two completly different ones!
"Game Maker For Life, probably never professional thou." =)
If they're totally different files, then no, it shouldn't be a problem. More likely it's something to do with the structures you modify when you store the loaded data, which may be shared.
You are linking with the multi-threaded runtime library aren't you?
Hmm... I'm currently linking to <fstream>.
I didn't know there was a multithreaded version! What would that be?
If it is possible to solve this way, let me know!

P.s

I've worked around the problem for now - I simply put all loading functions into a separate thread that loops through each loader one at a time.

/Robert
"Game Maker For Life, probably never professional thou." =)

This topic is closed to new replies.

Advertisement