Skip to content

Instantly share code, notes, and snippets.

@foobit
Created April 1, 2014 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foobit/9921345 to your computer and use it in GitHub Desktop.
Save foobit/9921345 to your computer and use it in GitHub Desktop.
from: Herb Sutter's - Going Native 2013
std::shared_ptr<widget> get_widget(int id)
{
static std::map<int, std::weak_ptr<widget>> cache;
static std::mutex m;
std::lock_guard<std::mutex> hold(m);
auto& wp = cache[id];
auto sp = wp.lock();
if (!sp)
{
wp = sp = load_widget(id);
}
return sp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment