Frage

I'm using MonoGame, but this is related more to winrt async and IO, so please don't redirect to gamedev.

I want to load game content asynchronously so that I can update the progress of a loading screen. I placed each ContentManager.Load call inside a Task and then inside my BeginLoading method, I just iterate over all of the tasks and start them in one shot.

This seems to work from my testing so far, but my question is whether or not this scales.

If I add 100 assets for async, this will spin off 100 tasks that will read from the disk al at the same time. Does WinRT scale like that?

In fact, should I just limit the number of tasks to the number of cores/physical treads? Or should I just have only one active Task altogether?

War es hilfreich?

Lösung

Creating 100 tasks will not necessarily start 100 concurrent threads that all execute at the same time. You're limited to the number of pool threads available and also to what the TPL things is a reasonable number of tasks to be running concurrently. On a 4-core machine, it's likely you won't get more than three or four concurrent tasks.

That said, you're probably better off starting a single task that then iterates over your list of assets and loads them one at a time. With multiple threads making I/O requests to the same drive, it's likely that those threads will spend a lot of time waiting on the disk and don't do much in the way of concurrent processing. You're getting little to no benefit from having many threads do the loading, because they spend most of their time idle. I wouldn't be surprised to find that your multiple loader threads take longer to complete than if you started a single thread that loads all the assets one at a time.

I don't see how creating 100 individual tasks, each one loading a single asset, can be any easier to code than a single task that loads those 100 assets sequentially. I can't think of how you could create such a data structure.

In short, you don't have to limit your number of tasks to the number of available cores; the TPL will do that for you. But you're probably better off having a single task. At best, you'd be marginally better off with multiple concurrent threads, but it's unlikely.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top