Jump to content
  • Advertisement
Sign in to follow this  
synth_cat

bad to have many files open at once?

This topic is 3790 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

My game currently uses streaming music. Whenever I switch from one song to another (which involves closing the first file and opening/initializing the second one) it causes a nasty lurch in the game. Is it OK for me to just open all the files at the start of the game and keep them open continuously, just streaming from one at a time? I would be having around 10-20 files open. I am using Windows, dealing with files opened via fopen() style routines. Thanks! -Greg

Share this post


Link to post
Share on other sites
Advertisement
Each process has a limit on the number of files it may have open at once. That limit is much much higher than 10-20, so you should be fine.

The best performance can be gained by loading all sound files into memory at once and closing all your open file handles. You should do that if you can afford it (memory-wise).

Share this post


Link to post
Share on other sites
What are the risks to keeping my files open the whole time? (I can't afford to load all the data into memory - it's far too much.)

Share this post


Link to post
Share on other sites
Quote:
Original post by KodeNerd
It would probably be better to close them as they could cause nasty problems on some rare chance.


For example?

Share this post


Link to post
Share on other sites
Some other thread can try to access the same file, for example, and I don't think it is possible to access a file from more than one application. The handle could actually become invalid during execution and may just be accessed without error checking. That is all I can think of right off the top of my head.

Share this post


Link to post
Share on other sites
But is it any riskier to have 10 files open continuously than it is to have one file open continuously? Because I'm going to have to have one file open continuously no matter what, seeing as I'm streaming music.

Share this post


Link to post
Share on other sites
I would stream until the end of the file and then close it.

I am not entirely sure if it is riskier (though my mind says it could be a very big risk) as I have not extensively tested it.

Share this post


Link to post
Share on other sites
I think the culprit here is blocking I/O, which is what fopen is. This means in most implementations that all threads (maybe just the calling thread?) in a process will be halted while the I/O is performed. This is causing the lurch in your game.

In order to get around this, you need to use platform-specific asynchronous file I/O methods. Basically, these methods will not guarantee that the data is read or written to disk when the function call returns. Usually, one will be notified of the completion of a disk read or write through a callback method or event.

Share this post


Link to post
Share on other sites
Quote:
Original post by KodeNerd
Some other thread can try to access the same file, for example, and I don't think it is possible to access a file from more than one application. The handle could actually become invalid during execution and may just be accessed without error checking. That is all I can think of right off the top of my head.
That's guessing and speculation.
If you open the file with the GENERIC_READ, FILE_SHARE_READ passed to CreateFile then other programs can also open the file. Otherwise the other programs will simply fail to be able to open the file.

Feel free to keep them all open at once, 20 files is just fine. However, I wouldn't be too surprised if it doesn't help much, unfortunately. I suspect that the 3rd party library you are using causes the lurch because it tries to preload a decent chunk of the start of the file when you tell it to start playing.

Also check that those files aren't simply hugely fragmented on your disk.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!