View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Multithreaded Resource Manager

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1nekitusan  Members

Posted 28 November 2013 - 03:11 PM

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.

### #2TheComet  Members

Posted 28 November 2013 - 03:25 PM

That seems clean to me. The other, less complicated way, would be to poll the resource and check if it has loaded yet or not. I believe this is the way Ogre handles it as well:

"I would try to find halo source code by bungie best fps engine ever created, u see why call of duty loses speed due to its detail." -- GettingNifty

### #3Hodgman  Moderators

Posted 28 November 2013 - 03:29 PM

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, 28 November 2013 - 03:30 PM.

### #4nekitusan  Members

Posted 28 November 2013 - 03:36 PM

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

### #5SeanMiddleditch  Members

Posted 28 November 2013 - 10:36 PM

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).

Game Developer, C++ Geek, Dragon Slayer - http://seanmiddleditch.com

C++ SG14 "Games & Low Latency" - Co-chair - public forums

Wargaming Seattle - Lead Server Engineer - We're hiring!

### #6nekitusan  Members

Posted 29 November 2013 - 04:06 AM

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.

### #7AllEightUp  Moderators

Posted 29 November 2013 - 11:12 AM

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.

Most OS's supply API's with better exposure of this underlying IO which, typically, when used means you can go back to a single threaded solution and get the gains.  Win32 supplies the overlapped IO flags, multiple source/target buffers and the oh so overly complicated completion ports (and buggy up till about Vista).  Most *nix variations have AIO or for Linux you have epoll which I believe is the latest/greatest version right now and BSD variations have kevent/kqueue.

There are of course variations, limitations and often goofball implementation details involved with all of these.  Fortunately, with a bit of work the result is quite often a pretty massive latency reduction and much higher utilization of the physical media throughput rates.  This even applies to SSD's due to how their access is performed by the controllers.  If dealing with this is worth it, that's up to your game, but really high performance resource systems generally need to use at least the lowest variation supplied which is just a bit more complicated than fread.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.