Sign in to follow this  
Rasmadrak

Thread-safe file input (c++) ?

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this