Posted By: hufuzhipeng | Oct 7th @ 11:20 PM
page 1 of 1
Comments: 2 | Views: 469
I am working on a 3D game which needs to load lots of 3D-resource when players come to new place.  In order to keep game playing smoothly, I tried to load resource asynchronously.

Multiple-threads is a good choice, however, I needs to hander many things by myslef.

So, I tried ReadFileEx. According to the MSDN, "The ReadFileEx function reads data from a file asynchronously. It is designed solely for asynchronous operation". 

To my opinion, "asynchronous" means the function will return immediately so that I can do other things until the callback function will be called.

To my surprise, when I first call ReadFileEx to load one file, it will take long time (about hundreds milliseconds) to return !  It's not acceptable for a online game. I think ReadFileEx was doing some initializing work, such as finding the file on disk, setting up DMO, etc and these works take long time to complete.

So, it means I must use multiple-threads instead of asynchronous IO to handle this problem. Am I right? Any suggestion will be apprieciable.

evildictaitor
evildictaitor
How could you use the adjective "indescribable" truthfully?
The ReadFileEx function is the way to do asyncronous reading, but if you're writing an application such as a game, you should be explicitly managing your threads because you'll have more "things to do" than processors on your target machine. One of these threads should be responsible for loading in resources and agressively caching data, and it should do so syncronously - your other threads should be concerned with the update loop, playing sound and handling network input respectively.

On the other hand, ReadFileEx really shouldn't be taking more than tens of milliseconds to return.
I think it may help if you fire up Process Explorer then look how "DPCs" goes when you load the file... You may also see the individual thread and find out whether the blocking thread is the thread you expected.
page 1 of 1
Comments: 2 | Views: 469