Sign in to follow this  

Thread-Safe?

This topic is 3595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hiya, I've written a abstract file-streaming class for my current project, so that I can load assets from different sources, such as directly from disk or from a packed file, using a common interface. Some parts of the project are threaded (streaming audio for example) - is the Microsoft implementation of std::ifstream likely to be thread-safe? The way I've designed the mechanism is that an 'Archive' class will open a packed file from disk using std::ifstream, and a factory method can then be used to create 'file streams' for individual file inside it. I'm not sure what will happen if two threads try to read data from the same archive at the same time though. For example, if one thread is half-way through reading, and another thread jumps in and seeks to the location of the data it needs. Is std::ifstream thread-safe? Thanks for any help! [smile]

Share this post


Link to post
Share on other sites
Anything can be used in a threaded application; it's how you are using it that defines it's "thread safety". Typically you call an object/container "thread-safe" if it is safe to concurrently access the same instance from multiple threads simultaneously. In this respect, std::ifstream is almost certainly not threadsafe.

However, if you are just using it in one thread and not trying to access the same stream instance concurrently another then it's fine; windows handles the thread safety of the file system. There may be weird things happening if you attempt to open and modify the same file in multiple concurrent threads...

-me

Share this post


Link to post
Share on other sites
Thanks for the quick reply! I should have guessed it wouldn't be.

Two threads reading from the same ifstream will likely need to run concurrently - can anyone suggest a way of handling this? I could use a mutex around the stream, but I'm not sure how much that will affect performance.

EDIT:

It seems that as long as I don't lock the archive file, I can open more than one std::ifstream from it - this means I could give each FileStream class it's own ifstream to read from. Would there be any obvious problems with this?

Thanks [smile]

[Edited by - beebs1 on February 7, 2008 5:34:49 PM]

Share this post


Link to post
Share on other sites
Several threads READING from the same file is not really a problem. The only issue I see with that is performance because if the file you're reading from is fairly big, the simultaneous reads will probably mess up the OS cache.

Share this post


Link to post
Share on other sites
Quote:
Original post by Harry Hunt
Several threads READING from the same file is not really a problem. The only issue I see with that is performance because if the file you're reading from is fairly big, the simultaneous reads will probably mess up the OS cache.


Indeed. There are no benefits to be gained from arbitrary concurrent access to file.

The reason: Disk access is sequential!

If you need to have multiple threads accessing file information, look into overlapped operations under windows, or use emulated async approach via software thread-pool.

But concurrency doesn't work with disk, at least not without good knowledge of file system and HDD architecture, neither of which are viable to implement into this type of application.

This is the reason why Windows pro-actor approach works well for disk access.

Share this post


Link to post
Share on other sites

This topic is 3595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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