• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
nekitusan

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
{
    void onResourceLoaded(Resource* pRes)
    {
       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
    resourceManager()->addObserver(&myObserver);
    // async loading, returns right away, the resource will be loaded later on
    resourceManager()->load(myTextureId);
    // or we could do: resourceManager()->load(someTextureId, &myObserver);
}

Any other ideas would be welcomed, thanks.

 

0

Share this post


Link to post
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 = ...
Asset* asset = resourceManager()->load(assetName, onLoaded);
void Scene::Precache()
{
  m_asset = resourceManager()->load(assetName);
}
bool Scene::PollLoaded()
{
  bool loaded = m_asset->IsLoaded();
  return loaded;
}

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!
}} onLoaded;
resourceManager()->load("foo.texture", onLoaded);
//Worker thread:
template<class Fn> void LoadTexture( const char* name, Fn& onLoaded )
{
  Texture* tex = ...
  //load the texture...
  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 this post


Link to post
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 this post


Link to post
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).

 

I have a more complex job system that the resource system uses.  Job can be dispatched to a named thread pool (with the resource system having a dedicated thread "pool" with only one thread), callbacks can be registered for individual jobs or for job groups, and the callback can be flagged as being destined for the main thread, any available thread in the pool, or the thread that registered the callback.  Threads can query a threadpool and execute any queued jobs/callbacks allowed on that thread, which the main thread of course does in its main loop and other threads can do at their discretion.  Worker threads in thread pools just deque items as their thread loop.  This system has a huge benefit outside of just resource loading, too, which is partly why something vaguely similar is likely to be added to C++17 or a concurrency TS before that.

1

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0