Followers 0

# Multithreaded Resource Manager

## 6 posts in this topic

I have some conceptual problems for a resource manager to be thread safe (can be called from any thread). The manager has a list of worker threads (one per bundle, a bundle is a zip file).
Traditionally, in a single-threaded manager we do something like this:

// sync load of a texture, loads and returns after texture is fully loaded
Texture* pTex = resourceManager()->load("maka.png");
// we got the texture, use it
int w = pTex->width();
int h = pTex->height();
// do something with w and h

But in an async manager how we do that?
Currently I'm using an observer list with callbacks for when a resource is completely loaded, the Observer is called and you can use the pointer to the resource. In this manager, resources are used as IDs, you do not have direct access to the pointer because you do not know in what state is at some point, it may be removed or modified from another thread.
ResourceId myTextureId = 3493; // or we can use: resourceId("maka.png") to get the id

struct MyResObserver : public IResourceObserver
{
{
Texture* pTex = dynamic_cast<Texture*>(pRes);

if (pTex && pTex->id() == myTextureId)
{
int w = pTex->width();
int h = pTex->height();
// use w and h
}
}
} myObserver;

///....................

void someClass::someInit()
{
// we add our observer into the manager's list
// async loading, returns right away, the resource will be loaded later on
// or we could do: resourceManager()->load(someTextureId, &myObserver);
}


Any other ideas would be welcomed, thanks.

0

##### Share on other sites

The main two choices are callbacks, or polling. You can use a callback across all assets as you've shown above, or you can attach callbacks to specific assets. Alternatively, you can allow the user of the resource manager to continually query whether an asset has finished loading yet or not.

onLoaded = ...

void Scene::Precache()
{
}
{
}

One issue that you have to be careful with regarding callbacks, is which thread calls the callback!

//main thread
struct Game
{
Foo gamestate;
};
Game g_game;
...
struct { void operator()(Texture* tex) {
g_game.gamestate.DoStuff(tex); // callback modifies the game-state directly, which is owned by the main thread!
resourceManager()->load("foo.texture", onLoaded);
//Worker thread:
template<class Fn> void LoadTexture( const char* name, Fn& onLoaded )
{
Texture* tex = ...
onLoaded( tex ); // Uh oh! Worker thread calls the callback, which creates race conditions with the main thread!
}

To avoid these kinds of problems, if you require callbacks to be called by a certain thread, then the workers can queue them up, and then the main thread can call resourceManager->PollForCallbacks() etc, which goes ahead and executes all the callbacks that have been queued up by the workers.

Edited by Hodgman
2

##### Share on other sites

alright guys, thanks, Hodgman good point with the queue filled from any thread with "onloaded" messages to be consumed by the main thread.

0

##### Share on other sites
The manager has a list of worker threads (one per bundle, a bundle is a zip file).

Why so many threads?  The I/O subsystem can only efficiently read one file at a time anyway.  There's not necessarily a lot of benefit to having more than one thread when I/O bound.

You might find it way, way easier to use something like std::function<> instead of an interface for your callbacks.  It can significantly reduce the verbosity of your code.  Most STL implementations will do a small-function optimization so that std::function<> doesn't allocate, and if you're hardcore about game frameworks you can make your own that has a fixed static size (and raises a compilation error rather accepting something needing memory allocation to store).

1

##### Share on other sites

SeanMiddleditch, I was thinking of that, thread/bundle, and you're right, wouldn't help, even worse, since its serial reading from hdd, so I will have one thread for the resource manager, thanks for reassuring it :). I have also a job/task scheduler which will be used by the resource manager, but I think I will use polling since its more safe/clear for me at least, instead of callbacks (which I also support). I dont use STL :) for various reasons, more or less logical.

0

## 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