urho3d/Urho3D — pull request #109 by alexparlett (2014-01-03 21:17:34+00:00)
Title: Added Asynchronous Resource Loading using WorkItems.

Not the most elegant of solutions, but wanted to stick to using the WorkItem structure. Works along the following lines:

```C++
loadingResources_.Push("Objects/LightFlash.xml");
loadingResources_.Push("Objects/Ninja.xml");
loadingResources_.Push("Objects/Potion.xml");
loadingResources_.Push("Objects/SnowBall.xml");
loadingResources_.Push("Objects/SnowCrate.xml");

SubscribeToEvent(E_RESOURCELOADEDASYNC, HANDLER(Urho3DPlayer, HandleResourceLoaded));
```
```C++
ResourceCache* rc = GetSubsystem();

for (unsigned i = 0; i < loadingResources_.Size(); ++i)
{
rc->LoadResourceAsync(loadingResources_[i]);
}
```
```C++
using namespace ResourceLoadedAsync;

loadingResources_.Remove(eventData[P_NAME].GetString());

if (!loadingResources_.Size())
{
...
}
```
It was done this way because firstly, there is no way to use the volatile bool from the WorkItem to track when a WorkItem has been completed because as soon as your copy is added to the WorkQueue its switched from your created WorkItem to one added to the Main Thread container. *This may just be from extreme lack of sleep in my testing, but I think thats the case.*

Secondly since references and iterators for the container holding what resource to load can be invalidated at anytime by removing or adding there was no point passing them with the WorkItem.

I did toy with the idea of limiting the number of mutexs in general operation and adding all resources that have been completed to the resources groups at the start of the next frame and so not needing mutexs on the resource groups at all, but considering how much the ResourceCache will be used during actual game play, I don't think it will be an issue (please correct me here if you feel differently).